[英]Unable to add parent entity with existing child entities in EF4
我正在使用通用存儲庫作為存儲庫中的DataLayer。 現在,我將創建一個新實體(訂單),並根據用戶選擇的值向其添加OrderDetail。
每個OrderDetail與Product都有一個->一個關系。 用戶選擇一種產品,然后將其添加到已添加到訂單中的OrderDetail中。
現在,Order和OrderDetail是“新”對象,但是Product(及其相關實體)從我的數據庫中檢索並添加到OrderItem(當時是一個)中。 因此,它們被附加到DynamicProxy(我的上下文是由通用存儲庫創建的)
我總是收到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;
}
}
}
當您添加圖時,EF將更改實體的狀態,即使它們已經被跟蹤。 我相信在您的情況下,因為您添加了一個實體,所以相關實體的狀態(在上下文中已被跟蹤)將變為添加。 您可以做的是先添加新實體,然后在添加實體后設置關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.