hazjack
hazjack

Reputation: 1715

EF6 How to avoid creating tables for entities managed by another DbContext?

I have below structure of classes:

  1. SecurityLayer.Domain dll

    • User class and some other classes
    • Has a nuget package for this
  2. SecurityLayer.Data dll

    • SecurityDbContext class (which inherits from DbContext and manages User related entities
    • Has a nuget package for this

Now I build another product which uses two packages above. And I have below structure

  1. MyProduct.Data dll has a ProductDbContext to manage entities from MyProduct.Domain dll

The problem is when I use Add-Migration on MyProduct.Data project, EF generates a migration to create ALL entities from both MyProduct.Domain and SecurityLayer.Domain

I know that EF6 support multiple DbContext in a single database, but it does not work for me. Can we do something to let EF avoid entities in SecurityLayer.Domain? Any advice is much appreciate. Tks a lot!

Upvotes: 1

Views: 309

Answers (1)

DavidG
DavidG

Reputation: 119156

The easiest way would be to create your initial migration and remove the bits from it that are not relevant. Subsequent migrations will not pick up the existing tables due to the way EF works.

Migrations actually store the state of the database in a table called __MigrationHistory. One of those columns contains a zipped up EDMX file. You can extract the content as a blob, save it as a .zip file and see for yourself. Each migration uses that to determine what has changed since the previous migration.

Upvotes: 1

Related Questions