首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在EF4中添加具有现有子实体的父实体

无法在EF4中添加具有现有子实体的父实体
EN

Stack Overflow用户
提问于 2014-03-19 13:35:12
回答 1查看 139关注 0票数 0

我正在使用一个通用存储库作为我的存储库中的DataLayer。现在,我正在创建一个新的实体(Order),并根据用户选择的值向其添加OrderDetail。

每个OrderDetail有一个->,一个与Product的关系。用户选择一个产品,我将其添加到OrderDetail中,然后添加到订单中。

现在,Order和OrderDetail是“新”对象,但是产品(及其相关实体)从我的数据库中检索并添加到OrderItem (当时只有一个)。因此,它们被附加到一个DynamicProxy (我的上下文是由泛型存储库创建的)

我总是得到IEntityChangeTracker的错误:

实体对象不能由多个IEntityChangeTracker实例引用

现在,我知道问题在于DbContext ...but,我仍然无法找到任何解决方案,有什么想法吗?

这是我的DbContext课

/这是数据访问层管理类。/公共部分类MyDataLayer {/这是我们将该类的实例存储在。/在属性中使用。/私有静态只读字符串UOW_INSTANCE_KEY = "MyDataLayer_Instance";

代码语言:javascript
复制
    /// <summary>
    /// This is used for thread-safety when creating the instance of this class to be stored in
    /// the UnitOfWorkStore.
    /// </summary>
    private static readonly object s_objSync = new object();

    // The DataContext object
    private readonly ITTEntities m_context;



    // ********************************************************************************
    // *** Constructor(s) *************************************************************
    // ********************************************************************************

    /// <summary>
    /// Default constructor.  Creates a new MyEntities DataContext object.
    /// This is hidden (private) because the instance creation is managed as a "unit-of-work", via the
    /// <see cref="Instance" /> property.
    /// </summary>
    private MyDataLayer()
    {
        m_context = new ITTEntities();
    }



    // ********************************************************************************
    // *** Public properties **********************************************************
    // ********************************************************************************

    /// <summary>
    /// The ObjectContext object that gives us access to our business entities.
    /// Note that this is NOT static.
    /// </summary>
    public ITTEntities Context
    {
        get { return m_context; }
    }


    /// <summary>
    /// This will get the "one-and-only" instance of the MyDataLayer that exists for the lifetime of the current "unit of work",
    /// which might be the lifetime of the currently running console application, a Request/Response iteration of an asp.net web app,
    /// an async postback to a web service, etc.
    /// 
    /// This will never return null.  If an instance hasn't been created yet, accessing this property will create one (thread-safe).
    /// This uses the <see cref="UnitOfWorkStore" /> class to store the "one-and-only" instance.
    /// 
    /// This is the instance that is used by all of the DAL's partial entity classes, when they need a reference to a MyEntities context
    /// (MyDataLayer.Instance.Context).
    /// </summary>
    public static MyDataLayer Instance
    {
        get
        {
            object instance = UnitOfWorkStore.GetData(UOW_INSTANCE_KEY);

            // Dirty, non-thread safe check
            if (instance == null)
            {
                lock (s_objSync)
                {
                    // Thread-safe check, now that we're locked
                    if (instance == null) // Ignore resharper warning that "expression is always true".  It's not considering thread-safety.
                    {
                        // Create a new instance of the MyDataLayer management class, and store it in the UnitOfWorkStore,
                        // using the string literal key defined in this class.
                        instance = new MyDataLayer();
                        UnitOfWorkStore.SetData(UOW_INSTANCE_KEY, instance);
                    }
                }
            }

            return (MyDataLayer)instance;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-19 17:50:35

添加图表时,EF将更改实体的状态,即使它们已经被跟踪。我相信在您的情况下,因为您添加了一个实体,相关实体的状态(在上下文中已经被跟踪)将更改为add。您可以做的是首先添加新实体,并在添加实体后设置关系。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22507588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档