【发布时间】:2011-07-04 22:37:29
【问题描述】:
这个错误抛出了很多,但我找不到解决办法。我是实体框架的新手,在我的第一种方法中我遇到了这个错误。
这就是我所拥有的。我有一个公司班和一个分公司班。这两个类都有自己的存储库。一个公司有一个分公司,一个分公司可以有多个公司。
在我的 GUI 中,我使用从 BranchRepository 获取的 Branch 对象填充组合:
public IList<Branch> GetAllBranches()
{
var query = _context.Branches;
IList<Branch> branches = query.ToList();
return branches;
}
这是结果是分支组合框的数据源。
当我想拯救公司时,我会这样做:
company.VisitorAddress = txtVisitAddress.Text;
company.City = txtCity.Text;
company.CompanyName = txtCompany.Text;
company.PhoneNumber = txtPhoneNumber.Text;
company.ZipCode = txtZipcode.Text;
company.Branch = ((Branch)cmbBranches.SelectedItem);
company.Website = txtWebsite.Text;
然后,我调用我的公司存储库来保存我的公司。保存方法如下所示:
public bool Save(Company company)
{
_context.AddToCompanies(company); // <-- This is where the error is thrown.
_context.SaveChanges();
return true;
}
当调用 save 方法时,我收到错误“一个实体对象不能被多个 IEntityChangeTracker 实例引用”。
显然我做错了什么,但是什么?
【问题讨论】:
标签: c# entity-framework