How to change the schema for Entity Framework Code First

By default Entity Framework will call the dbo user/schema when attempting to communicate with the database.  Today I received an exception because the tables I was calling was not [dbo].[Customer] but [Sales].[Customer].

This of course would also work if you do not have a pre-existing database.

To change this in the fluent API this can be changed in the modelBuilder by adding the following:

modelBuilder.Entity<Customer>().ToTable(“Customer”,”Sales”);

When using data annotations add the following:

[Table(“Customer”, Schema = “Sales”)]
Public Class Customer

Advertisement
This entry was posted in Entity Framework and tagged , , . Bookmark the permalink.