Reputation: 843
I use EntityFramework 6 with code first and i am trying to solve a problem with two contexts and overlapping entities.
Example:
Context c1 has the entities A and B
Context c2 has the entities B and C
Entity B is in c1 and c2 the same entity
Entity B has a many to many relation to entity A
Entity B has also a many to many relation to entity C
c1 c2
(A -- B)(B -- C)
I tried to solve this problem with inheriting entity B in the second context with a child class and add the relation to entity C there.
In my current approach EF tells me that the database already has the entity B (from update-database of c1) and it will stop updating context c2.
Someone got a solution or a complete different approach?
Upvotes: 2
Views: 630
Reputation: 8474
When modelling the database using Entity Framework, sharing items with each other in different DbContext
is actually a limitation with the EF implementation. The problem is down to EF using proxies. That is, it actually sub-classes your class with something to monitor your interaction with the database. If you move tracking from one DbContext
to another, it will complain that you are attaching something that doesn't exist - or isn't being currently monitored.
1 approach is to just bundle it all into 1 DbContext
- problem solved.
If you really want to divide them apart physically, you will have to move entities from one to the other manually. You will have to .Detach()
and .Attach()
them manually, which seems like a code-smell to me.
Upvotes: 1