"一个实体对象不能由IEntityChangeTracker&QUOT的多个实例被引用; [英] "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
问题描述
我有3个数据库表:供应商,各地区,各VendorRegions:
I have 3 database tables: Vendors, Regions, and VendorRegions:
Vendors
-------------------
VendorID (PK)
Name
Regions
-------------------
RegionID (PK)
Name
VendorRegions
-------------------
VendorID (PK)
RegionID (PK)
在我的网页创建一个供应商,我列出了每区一个复选框列表。对于每一个被选中区域,我想的厂商ID和RegionID添加到VendorRegions表。
On my page to create a vendor, I list out every Region in a checkbox list. For every region that is checked, I'd like to add the VendorID and RegionID to the VendorRegions table.
我使用实体框架,C#和MVC 3。
I'm using Entity Framework, C# and MVC 3.
下面是我的控制器code:
Here's 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
}
}
下面是我的供应商库code:
Here's my Vendors repository code:
public class SqlVendorsRepository : IVendorsRepository
{
private readonly MyDBEntities _entities;
public SqlVendorsRepository()
{
_entities = new MyDBEntities();
}
public void SaveVendor(Vendor vendor)
{
// If it's 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's 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();
}
}
当我尝试保存我的供应商,在 _entities.Vendors.Context.AddObject(「卖方」,供应商)出现错误;
。以防万一,这是我区资源库code:
The error occurs when I try to save my vendor, at _entities.Vendors.Context.AddObject("Vendors", vendor);
. And just in case, here's my Regions 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 to do, but I don't know how to get past this error. Any ideas?
推荐答案
您有两个不同的版本库,每有一个独立数据上下文 - 这是根本的问题 - 你的数据上下文应该由共享所有的资料库,即你可以通过构造函数注入注入它:
You have two different repositories that each have a separate data context - this is the root problem - your data context should be shared by all your repositories, i.e. you can inject it via constructor injection:
MyDBEntities entities = new MyDBEntities();
_vendorsRepository = new SqlVendorsRepository(entities);
_regionsRepository = new SqlRegionsRepository(entities);
这篇关于&QUOT;一个实体对象不能由IEntityChangeTracker&QUOT的多个实例被引用;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!