![](/img/trans.png)
[英]The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects Entity Framework
[英]The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
我已經閱讀了一些與此特定錯誤消息有關的問題/答案,但我不太了解適當的解決方案。
我已經多次閱讀您應該創建 EF4 上下文,使用它,然后處理它。 在我的整個應用程序中,我使用不同的上下文對象在這里和那里加載實體,然后最終希望將這些實體關聯在一起。
我創建了一個簡單的控制台應用程序,它很容易導致錯誤。 繪制了非常簡單的模型,然后是代碼。
如何讓兩個不同的實體共享相同的上下文? 我真的必須創建一個新的上下文,再次加載這兩個實體(即使我已經有了它們),只是為了關聯它們並保存?
如果我只是錯過了一個已經存在的、合適的問題/答案,請把我指向正確的地方。
internal class Program {
private static void Main(string[] args) {
DeleteAllEntities();
CreateInitialEntities();
Owner o = LoadOwner();
Child c = LoadChild();
AssociateAndSave(o, c);
}
private static void AssociateAndSave(Owner o, Child c) {
using (var context = new ModelEntities()) {
// Exception occurs on the following line.
o.Children.Add(c);
context.Attach(o);
context.SaveChanges();
}
}
private static Owner LoadOwner() {
using (var context = new ModelEntities()) {
return ((from o in context.Owners
select o).First());
}
}
private static Child LoadChild() {
using (var context = new ModelEntities()) {
return ((from c in context.Children
select c).First());
}
}
private static void CreateInitialEntities() {
using (var context = new ModelEntities()) {
Owner owner = new Owner();
Child child = new Child();
context.Owners.AddObject(owner);
context.Children.AddObject(child);
context.SaveChanges();
}
}
private static void DeleteAllEntities() {
using (var context = new ModelEntities()) {
List<Child> children = (from c in context.Children
select c).ToList();
foreach (var c in children)
context.Children.DeleteObject(c);
List<Owner> owners = (from o in context.Owners
select o).ToList();
foreach (var o in owners)
context.Owners.DeleteObject(o);
context.SaveChanges();
}
}
}
您應該在同一上下文中獲得對子對象和所有者對象的引用。 一種方法是獲取 id,然后將它們作為參數傳遞給AssociateAndSave(int oId, int cId)
方法。
然后,您檢索對同一上下文中對象的引用並進行連接。
private static void Main(string[] args)
{
DeleteAllEntities();
CreateInitialEntities();
int oId = LoadOwnerId();
int cId = LoadChildId();
AssociateAndSave(oId, cId);
}
private static int LoadOwnerId()
{
using (var context = new ModelEntities())
{
return (from o in context.Owners
select o).First().Id;
}
}
private static int LoadChildId()
{
using (var context = new ModelEntities())
{
return (from c in context.Children
select c).First().Id;
}
}
private static void AssociateAndSave(int oId, int cId)
{
using (var context = new ModelEntities())
{
var owner = (from o in context.Owners
select o).FirstOrDefault(o => o.ID == oId);
var child = (from o in context.Children
select o).FirstOrDefault(c => c.ID == cId);
owner.Children.Add(child);
context.Attach(owner);
context.SaveChanges();
}
}
您不能使用另一個上下文的實例從一個上下文中刪除對象,因為每個上下文分別跟蹤其對象。
一個想法是為您的每個方法提供一個您的上下文類型的參數,以便您的操作在相同的上下文中進行。
前任:
private static void CrudOperationExample(DBContext contextInstane)
{
//perform crud operations.
}
我想做得更好,我建議你尋找依賴注入,因為它在 ORM 的 .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.