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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s