ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker(ADO.Net Entity Framework 一个实体对象不能被多个 IEntityChangeTracker 实例引用)
问题描述
我正在尝试保存我的联系人,其中引用了 ContactRelation(只是联系人、已婚、单身等的关系)和国家/地区.但是每次我尝试保存经过验证的联系人时,我都会收到异常ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
I am trying to save my contact, which has references to ContactRelation (just the relationship of the contact, married, single, etc) and Country. But everytime I try to save my contact, which is validated I get the exception "ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
public Contact CreateContact(Contact contact)
{
_entities.AddToContact(contact); //throws the exception
_entities.SaveChanges();
return contact ;
}
我正在使用带有服务和存储库的松散耦合 MVC 设计.我已经阅读了很多关于这个异常的帖子,但没有一个给我一个有效的答案......
I'm using a loosely coupled MVC design with Services and Repositories. I've read a lot of posts about this exception but none give me a working answer...
谢谢你,彼得
推荐答案
[更新]
因为使用 L2E,您需要先保存所有链接对象,然后才能保存主对象.这是有道理的,否则您将创建(在我的示例中)没有联系对象的艺术家.数据库设计不允许这样做.
[/更新]
这是我的实现.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")] Artist artist, [Bind(Prefix = "Contact")] Contact contact, [Bind(Prefix = "Country")] Country country, [Bind(Prefix = "ContactRelationship")] ContactRelationship contactRelationship)
{
ViewData["Countries"] = new SelectList(new CountryService(_msw).ListCountries().OrderBy(c => c.Name), "ID", "Name");
ViewData["ContactRelationships"] = new SelectList(new ContactRelationshipService(_msw).ListContactRelationships().OrderBy(c => c.ID), "ID", "Description");
country = _countryService.GetCountryById(country.ID);
contact.Country = country;
contactRelationship = _contactRelationshipService.GetContactRelationship(contactRelationship.ID);
contact.ContactRelationship = contactRelationship;
if(_contactService.CreateContact(contact)){
artist.Contact = contact;
if (_service.CreateArtist(artist))
return RedirectToAction("Index");
}
return View("Create");
}
然后在我的 ContactRepository 中:
And then in my ContactRepository :
public Contact CreateContact(Contact contact)
{
_entities.AddToContact(contact); //no longer throws the exception
_entities.SaveChanges();
return contact ;
}
我还在这个网站上发现最好在整个应用程序中保持相同的上下文,所以我现在为此使用了一个特殊的 Data 类:
Rick Strahl 和 Samuel Maecham 告诉我,您应该为每个请求的每个用户保留数据上下文.这意味着将其放入 Web 应用程序的 HttpContext 中.阅读所有相关信息 这里
Rick Strahl and Samuel Maecham have taught me that you should keep your datacontext per user per request. Which means putting it in the HttpContext for web applications. Read all about it here
public class Data
{
public static MyDBEntities MyDBEntities
{
get
{
if (HttpContext.Current != null && HttpContext.Current["myDBEntities"] == null)
{
HttpContext.Current["myDBEntities"] = new MyDBEntities ();
}
return HttpContext.Current["myDBEntities"] as MyDBEntities;
}
set {
if(HttpContext.Current != null)
HttpContext.Current["myDBEntities"] = value;
}
}
}
这篇关于ADO.Net Entity Framework 一个实体对象不能被多个 IEntityChangeTracker 实例引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ADO.Net Entity Framework 一个实体对象不能被多个 IEntityChangeTracker 实例引用


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01