【问题标题】:Unable to add parent entity with existing child entities in EF4无法在 EF4 中添加具有现有子实体的父实体
【发布时间】:2014-03-19 13:35:12
【问题描述】:

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

每个 OrderDetail 都与 Product 具有 one -> one 关系。用户选择一个产品,我将其添加到 OrderDetail 中,该 OrderDetail 被添加到 Order 中。

现在,Order 和 OrderDetail 是“新”对象,但 Product(及其相关实体)是从我的数据库中检索并添加到 OrderItem(当时一个)。因此它们被附加到一个 DynamicProxy(我的 Context 是由 Generic Repository 创建的)

我总是收到 IEntityChangeTracker 的错误:

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

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

这是我的 DbContext 类

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

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

【问题讨论】:

    标签: c# asp.net entity-framework c#-4.0 linq-to-entities


    【解决方案1】:

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

    【讨论】:

    • 是的,我已经尝试过这个并且 ui 可以工作......但我想尽量避免这种情况。我正在创建一个订单,并试图在客户确认购买之前不将其插入数据库。我在购买过程中将其存储在 Session 中
    • 另一种选择是恢复 EF 对现有实体的状态所做的更改。但它可能会更复杂,因为您必须在将新实体添加到上下文之前存储原始状态。
    • 我确实通过在添加 OrderItems 之前插入订单解决了这个问题。简单但不是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 2011-12-19
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-06-03
    • 2016-09-15
    相关资源
    最近更新 更多