I am creating an Entity Framework Code-First model to perform special queries on a SQL Server database. I do not include tables or views from the "dbo" schema in my EF model; instead, I only use tables / views from the "model" schema in my database. I have duplicate object names in my database that are separated only by a schema (for example, "dbo.Child" and "model.Child").
Is there one line that I can specify in a DbContext that will essentially “map all objects in this context to a“ model schema ”? I know that I can match each entity with a corresponding schema (see below), but I would like to avoid re-excluding all objects in my database.
This is what I know I can do:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>().ToTable("Child", "model");
modelBuilder.Entity<Referral>().ToTable("Referral", "model");
modelBuilder.Entity<Exit>().ToTable("Exit", "model");
}
This is what I would like to do:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new MapAllEntitiesToSchemaConvention("model"));
}
source
share