E
E
eRKa2016-07-31 10:23:11
ASP.NET
eRKa, 2016-07-31 10:23:11

Why doesn't EF want to persist the entity?

There is an ASP.NET WebApi application, Ninject is connected to it in this way calmsen.ru/nastraivaem-ninject-dlya-web-api/#more-166 , I have my own dataManager (UoW) with EF repositories inside, which I embed into the controller through the constructor . Everything seems to be good and beautiful. But when you try to do Add, you get an exception "An entity object cannot be referenced by multiple instances of the IEntityChangeTracker interface."
At first I sinned on automapper (from dto to entity) that because of it two contexts are created. But then I figured out by poke that the matter was in ninject. If I create the dataManager in the controller rigidly, via new, then everything is fine. If the dataManager is resolved via ninject, then an exception will be thrown.
Well, the question itself, who faced this and how to resolve it? I can't replace ninject. Could it be a connection problem? I already lack imagination, the second evening I sit with this garbage).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2016-07-31
@kttotto

Issue resolved. Indeed, two contexts were created. Only the error was not in ninject, but in my implementation of DataManger. It turned out that the entity was in one context, and the foreign key pointed to the entity in another context. That's it and did not want to be saved.
Thank you all for your participation.)

D
dmitrytut, 2016-07-31
@dmitrytut

Use Bind().To(). InRequestScope() instead of Bind().To(). InSingletonScope() .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question