Skip to content
Advertisement

Can I change the default schema name in entity framework 4.3 code-first?

Currently I am deploying my application to a shared hosting environment and code-first with migrations has been working great except for one minor hiccup. Everytime I want to push the site I have to use the “Update-Database -script” option because I have to prepend every table name with [dbo] because by default the shared host creates a default schema name that is the same name as the database username.

If I log into my shared host and create a database, I then have to create a user. If I name that user admin, then the tables code-first creates while logged in as admin look something like this “[admin].[BlogPosts]”. When the application runs all the tables are created but I get an EF exception because it says “[dbo].[BlogPosts]” is invalid. If I rename the table’s schema name to “[dbo]” instead of “[admin]” that fixes it.

To get around this I have to generate a migrations script to be executed manually and add “[dbo]” in front of all the table names because the script only references the tables by their name, not by their schema and their name.

Is there an easy way to get around this? It would be so nice if all I had to do was publish the application and everything just worked. If it wasn’t for the schema name discrepancy it would be a one click deploy and everything would be glorious.

Advertisement

Answer

You could use the ToTable method to specify the schema name. If you do not specify the schema name, EF will by convention use dbo.

public class MyContext
{
    private string schemaName = "Foo";

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<MyEntity>().ToTable("MyTable", schemaName);
    } 
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement