Dos entidades del mismo contexto
Frecuentes
Visto 582 equipos
2
I use EntityFramework 6 with code first and i am trying to solve a problem with two contexts and overlapping entities.
Ejemplo:
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?
1 Respuestas
1
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
- problema resuelto.
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()
y .Attach()
them manually, which seems like a code-smell to me.
Respondido el 03 de junio de 14 a las 16:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# ef-code-first entity-framework-6 or haz tu propia pregunta.
My current problem is before the step of doing stuff with the entities. With my mentioned design i am unable to set up the database (and therefore i am not able to use the entities). So i think your explenation descripes how to handle entities over multiple contexts, when the database is already set up. - sebastian