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

查看:93
本文介绍了ADO.Net实体框架实体对象不能被IEntityChangeTracker的多个实例引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存我的联系人,其中提到了ContactRelation(只是联系人,已婚,单身人士等的关系)和Country。
但是每次我尝试保存我的联系人,这是经过验证的,我得到异常ADO.Net实体框架实体对象不能被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...

谢谢你,
Peter


Thank you, Peter

推荐答案

[更新]

由于使用L2E,您需要保存所有在您可以保存主对象之前先链接对象。这是有道理的,否则你会创造(在我的例子中)艺术家没有它的联系对象。数据库设计不允许这样做。

[/ Update]

这是我的实现工作。 / p>

Here's my implementation which worked.

[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 ;
}

我也在本网站找到最好保留整个应用程序的上下文相同,所以我现在正在使用一个特殊的数据类:

Rick Strahl和Samuel Maecham告诉我,你应该保持你的每个请求每个用户的datacontext。这意味着将其放在HttpContext中用于Web应用程序。
阅读全文这里

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实体框架实体对象不能被IEntityChangeTracker的多个实例引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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