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

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

问题描述

我想救我的联系方式,其中引用了ContactRelation(接触的只是关系,结婚,单等)和乡村。 但每次我试图挽救我的接触,这是验证我得到的异常

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用于需要保存的所有链接的对象之前,首先你可以节省的主要对象。这是有道理的,否则你会创建(在我的例子),一个艺术家如果没有它的联系对象。这不是由数据库设计允许的。
[/更新]

[Update]
Because L2E is used you need to save all the linked objects first before you can save the main object. Which makes sense otherwise you would create (in my example) an artist without it's contact object. This isn't allowed by the database design.
[/Update]

下面是我的实现,它的工作。

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

<打击>我还发现在这个网站,最好是保持相同的情况下整个应用程序,所以我现在用的是特殊的数据类这样的:

里克施特拉尔和塞缪尔Maecham都告诉我,你应该让你的DataContext的每名用户的请求。这意味着把它在HttpContext的Web应用程序。 阅读所有关于它<一个href="http://sams$c$c.com/index.php/2009/12/making-entity-framework-v1-work-part-1-datacontext-lifetime-management/">here

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天全站免登陆