简体   繁体   English

如何以EF6代码优先的方式删除实体之间的关系?

[英]How can I remove relationship between entities in EF6 code-first?

I have one-to-many relationship between objects A and B. 我在对象A和对象B之间有一对多的关系。

public class A
{
    public int Id { get; set; }
    public int? OwnerId { get; set; }
    public B Owner { get; set; }
}

public class B
{
    public int Id { get; set; }
    public ObservableCollection<A> OwnedObjects { get; set; }
}

i have 我有

AutoDetectChangesEnabled = false;
ProxyCreationEnabled = false;

I want to delete A from OwnedObjects but not from database. 我想从OwnedObjects中删除A,而不是从数据库中删除。

this: 这个:

var b = Bs.Find(id);
Entry(b).Collection(_=>_.OwnedObjects).Load();
var someObjToRemove = b.OwnedObjects[0];
b.OwnedObjects.Remove(someObjToRemove);
Entry(b).State = EntityState.Modified;
SaveChanges();

doesn't help 没有帮助

this: 这个:

var b = Bs.Find(id);
Entry(b).Collection(_=>_.OwnedObjects).Load();
var someObjToRemove = b.OwnedObjects[0];
someObjToRemove.OwnerId = null;
Entry(someObjToRemove).State = EntityState.Modified;
SaveChanges();

throws exception: 引发异常:

A referential integrity constraint violation occurred: The property value(s) of 'B.Id' on one end of a relationship do not match the property value(s) of 'A.OwnerId' on the other end. 发生参照完整性约束冲突:关系一端的“ B.Id”的属性值与另一端的“ A.OwnerId”的属性值不匹配。

I can't delete object A and create another one without Owner because there is many other objects related with A 我不能删除对象A并在没有所有者的情况下创建另一个对象,因为还有许多其他与对象A相关的对象

It's seems to be not so hard to do. 似乎并不难做到。 But I can't find any solution 但我找不到任何解决方案

Ok. 好。 Only solution i found it's to call ChangeTracker.DetectChanges(); 我发现的唯一解决方案是调用ChangeTracker.DetectChanges(); after adding or removing items. 添加或删除项目后。

var itemToAdd = As.Find(a1.Id);
b.OwnedObjects.Add(itemToAdd);

var itemToRemove = As.Find(a2.Id);
b.OwnedObjects.Remove(itemToRemove);

ChangeTracker.DetectChanges();
SaveChanges();

works great. 效果很好。 but ChangeTracker.Entries() contains only instances of A modified. 但ChangeTracker.Entries() 包含A修改后的实例。 And if I replace ChangeTracker.DetectChanges() by 如果我将ChangeTracker.DetectChanges()替换为

Entry(itemToAdd).State = EntityState.Modified; 
Entry(itemToRemove).State = EntityState.Modified;

it doesn't work again. 它不再起作用。

So i think, calling ChangeTracker.DetectChanges(); 所以我认为,调用ChangeTracker.DetectChanges(); it is most simplest way to solve this problem 这是解决此问题的最简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF6 - 代码优先 - 如何正确处理从m:n关系中删除父条目 - EF6 - code-first - how to properly handle deleting a parent entry from an m:n relationship 如何以代码优先方式共享EF6对象 - How to share EF6 objects in code-first 使用EF6 Store Functions for Entity Framework代码优先,我可以返回自定义类型吗? - Using EF6 Store Functions for Entity Framework code-first, can I return a custom type? EF代码优先1:相同模型之间的1:* 1:0..1关系 - EF code-first 1:* 1:0..1 relationship between the same models 我可以使用并返回EF4代码优先的POCO实体作为其接口吗? - Can I use and return EF4 code-first POCO entities as their interface? 如何使3个实体之间的一对多关系与Entity Framework Core 2.1一起使用 - 代码优先 - How to make one-to-many relationship between 3 entities work with Entity Framework Core 2.1 - code-first 如何使用EF4 Code-First一次创建两个关联实体 - How do I create two associated entities at one time with EF4 Code-First 我将如何使用EF-5代码优先定义1-1关系 - How would I defined a 1-1 relationship using EF-5 code-first EF代码优先的RelationShip错误 - RelationShip error on EF code-first EF代码优先中的关系问题 - relationship problems in EF code-first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM