ADO.Net Entity Framework 一个实体对象不能被多个 IEntityChangeTracker 实例引用 [英] ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker

查看:23
本文介绍了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 实例引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆