문제

I am using a Generic Repository as an DataLayer in my repository. Now I am creating a new entity (Order), and adding OrderDetail to it based on the user selected values.

Each OrderDetail has a one -> one relationship to Product. User selects a product and I add this to a OrderDetail that get's added to a Order..

Now, Order and OrderDetail are "new" objects, but Product (and it's related entities) are retrieved from my database and added to a OrderItem (one at the time). Therefore they are attached to a DynamicProxy (my Context is created by the Generic Repository)

I always get the error of IEntityChangeTracker:

Entity object cannot be referenced by multiple instances of IEntityChangeTracker

Now, i know that the problem is with the DbContext ...but still I am not able to find any solution, any idea??

This is my DbContext class

/// /// This is the data access layer management class. /// public partial class MyDataLayer { /// /// This is our key to store an instance of this class in the . /// This is used in the property. /// private static readonly string 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;
        }
    }
}
도움이 되었습니까?

해결책

When you add a graph EF will change the state of the entities even if they are already tracked. I believe in your case since you add an entity the state of the related entities (that are already tracked in the context) will be changed to added. What you can do is to add the new entity first and set the relationships after the entity is added.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top