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

后端 未结 5 1822
甜味超标
甜味超标 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:30

    You can map each table to its own schema by fluent mapping only. In your DbContext subtype you should override OnModelCreating (if you haven't done so already) and add statements like this:

    modelBuilder.Entity()  
        .ToTable("t_Department", "school");
    

    Entities that you don't map like this explicitly will be placed in the default dbo schema, or you can provide your own default by

    modelBuilder.HasDefaultSchema("sales");
    

    (summarized from here)

提交回复
热议问题