0

My application is built on MVC and entity framework using unit of work pattern and DDD.

Below is the code flow in controller action method.

  1. create a unit of work instance .
  2. create a domain service instance by passing the unit of work object to domainService constructor.
  3. call the appropriate method using domainService object, this method makes necessary changes to the entity objects.
  4. do commit(saveChanges) on unit of work.

If there is any unhandled domain exception thrown in step 3 (All the exceptions were handled in a mvc attribute and exception message is displayed on UI). If user corrects the data on UI and clicks on save, I am getting the below error in subsequent call to same action method

Error: "entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetracker"

What happens to the DbContext if unhandled exception is thrown in web request? Is the context created in the previous web request remains active in the next web request? If yes, is that the reason why am getting the above exception?

If that's not the reason, will calling a dispose() on exception solves my problem ?

DivideByzero
  • 474
  • 4
  • 15
  • That type of error message in this type of scenario usually means you've put a live object (from an tracking-enabled ORM, for example) into an in-memory cache (like the asp.net cache), or some other shared state; is it possible that is the cause here? – Marc Gravell Jun 04 '16 at 20:32
  • @MarcGravell, yes I have used MemoryCache to store one of the look up data. since it is just a look up data, I amfetching the data using DbSet.AsNoTracking().This data is used only to display data in the view, so I assume change tracker should not be tracking this entity. – DivideByzero Jun 04 '16 at 20:37
  • Possible duplicate of [entity object cannot be referenced by multiple instances of IEntityChangeTracker. while adding related objects to entity in Entity Framework 4.1](http://stackoverflow.com/questions/10191734/entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetracker) – NightOwl888 Jun 05 '16 at 14:19
  • @NightOwl888 this is not a duplicate of the question you have referred to. Thought the error might be same in both the questions, the root cause is different. In my case, I am not creating context instance within the service class, whereas the user in the above link has created separate instance of DbContext in each of his services(which resulted in the error). – DivideByzero Jun 06 '16 at 16:40

0 Answers0