Setting schema name for DbContext

c# database-schema entity-framework entity-framework-6

Question

I know how to set the schema for a table in my context but is there a way to set the default schema for all the tables in a context? i.e.

[Schema = "Ordering"] 
public class MyContext:DbContext
{
    public MyContext()
        : base("name=ConnectionString")
    {
    }

    public DbSet<Student> Students { get; set; }
}
1
16
5/28/2015 4:40:57 PM

Accepted Answer

You can configure the default schema in OnModelCreating method of your custom inherited DbContext class like -

public class MyContext: DbContext 
        {
            public MyContext(): base("MyContext") 
            {
            }

            public DbSet<Student> Students { get; set; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //Configure default schema
                modelBuilder.HasDefaultSchema("Ordering");
            }
        }

Starting with EF6 you can use the HasDefaultSchema method on DbModelBuilder to specify the database schema to use for all tables, stored procedures, etc. This default setting will be overridden for any objects that you explicitly configure a different schema for.

24
5/28/2015 4:37:44 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow