"An entity object cannot reference multiple instances of IEntityChangeTracker"

I have 3 database tables: providers, regions, and VendorRegions:

Vendors ------------------- VendorID (PK) Name Regions ------------------- RegionID (PK) Name VendorRegions ------------------- VendorID (PK) RegionID (PK) 

On my page, to create a provider, I list each region in the checkbox list. For each region being checked, I would like to add VendorID and RegionID to the VendorRegions table.

I am using Entity Framework, C # and MVC 3.

Here is my controller code:

 public class VendorsController : Controller { readonly IVendorsRepository _vendorsRepository; readonly IRegionsRepository _regionsRepository; public VendorsController() { _vendorsRepository = new SqlVendorsRepository(); _regionsRepository = new SqlRegionsRepository(); } [HttpPost] public ActionResult Create(VendorViewModel viewModel, string[] Regions) { var vendor = new Vendor(); TryUpdateModel(vendor); if (ModelState.IsValid) { vendor.Regions.Clear(); foreach (var regionId in Regions) { vendor.Regions.Add(_regionsRepository.GetRegion(Int32.Parse(regionId))); } _vendorsRepository.SaveVendor(vendor); return RedirectToAction("Index"); } return View(viewModel); // validation error, so redisplay same view } } 

Here is my Vendors repository code:

 public class SqlVendorsRepository : IVendorsRepository { private readonly MyDBEntities _entities; public SqlVendorsRepository() { _entities = new MyDBEntities(); } public void SaveVendor(Vendor vendor) { // If it a new vendor, just attach it to the DataContext if (vendor.VendorID == 0) _entities.Vendors.Context.AddObject("Vendors", vendor); // ERROR HERE else if (vendor.EntityState == EntityState.Detached) { // We're updating an existing vendor, but it not attached to this data context, so attach it and detect the changes _entities.Vendors.Context.Attach(vendor); _entities.Vendors.Context.Refresh(System.Data.Objects.RefreshMode.ClientWins, vendor); } _entities.Vendors.Context.SaveChanges(); } } 

The error occurs when I try to save my provider in _entities.Vendors.Context.AddObject("Vendors", vendor); . And just in case, here is my region repository code:

 public class SqlRegionsRepository : IRegionsRepository { private readonly MyDBEntities _entities; public SqlRegionsRepository() { _entities = new MyDBEntities(); } public IQueryable<Region> Regions { get { return _entities.Regions.AsQueryable(); } } public Region GetRegion(int id) { return Regions.FirstOrDefault(st => st.RegionID == id); } } 

This seems like a simple thing, but I don't know how to get through this error. Any ideas?

+4
source share
2 answers

You have two different repositories, each of which has a separate data context - this is the root problem - your data context should be used by all your repositories, i.e. you can inject it through constructor injection:

 MyDBEntities entities = new MyDBEntities(); _vendorsRepository = new SqlVendorsRepository(entities); _regionsRepository = new SqlRegionsRepository(entities); 
+11
source

I came across the same error when I had a Caching layer that cached search entities and retrieved them from the cache. The cached objects were proxies and still had a link to the DBC text that originally extracted them.

The main symptom of this is that the first saving of the Domain object using the help reference worked fine, but the subsequent ones could not be saved.

Hope this helps someone as the problem has gone out of my life for 2 days.

+2
source

All Articles