Using Entity Framework 6 with Multiple DB Schemas but using One DBContext

后端 未结 5 1818
甜味超标
甜味超标 2020-12-13 17:43

I have an application using EF as ORM. The database used to have one schema, dbo and everything was working fine. I recently organized my tables into 4 different schemas. So

5条回答
  •  时光取名叫无心
    2020-12-13 18:20

    @GertArnold is spot on with his answer. However for pure syntactic candy you can also do this via a convention to pull the schema from the namespace of your models. We found this useful dealing with multiple schemas

    modelBuilder.Types().Configure(e => {
            var schema = e.ClrType.Namespace.Split('.').Last().ToLower();
            var name = entity.ClrType.Name;
            return entity.ToTable(name, schema);
    });
    

    the above will take the final component of the namespace and use it as the schema name. This avoids the need for customising the table binding for every entity.

提交回复
热议问题