从多对多关系中仅删除一个条目 [英] Deleting only one entry from Many-to-Many relationship
问题描述
好,所以我现在有这个问题。我有2个像这样的模型:
Ok, so i have this problem right now. I have 2 Models like so:
public class Contact: BaseModel
{
public string LastName { get; set; }
public string FirstMidName { get; set; }
public string Adress { get; set; }
public int UserID { get; set; }
[Display(Name = "Full Name")]
public string FullName
{
get
{
return FirstMidName+ " " + LastName;
}
}
public virtual List<Group> Groups { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Phones> Phones { get; set; }
}
,并且:
public class Group:BaseModel
{
public string Name { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public virtual List<Contact> Contacts { get; set; }
}
这个想法是,许多联系人可以在多个组中。但是我的问题来了。我将公约ondeleteCascade删除了很多,因为我不得不否则我的代码将无法正常工作。但是...我如何只删除一个联系人...而无需级联并删除它的所有组中以及该组包含的所有联系人,依此类推。这就是我的问题,我希望能够删除一个联系人而不是一个群组。我不希望仅删除联系人。请帮忙
the idea is that many contacts can be in many groups. But here comes my problem. I remove the convention ondeleteCascade with many to many because i had to otherwise my code wouldnt work.But... how can i delete only one contact... without it cascading and deleting all the groups it
s in and all the contacts that group contains and so on and so forth. Thats my problem i
d love to be able to delete one contact not a group tho. i do not want the group to be deleted just the contact. Please help
推荐答案
您只需将级联删除
选项保持为<
You should simply keep the cascade delete
option turned on
for that relationship.
在多对多链接,而不是实际的组实体。删除组时同样如此。
In the many-to-many
relationship, none of the sides "owns" the other. Instead, the relationship is maintained by a third entity (hidden in your case) called "link" table, so for instance deleting a contact will simple delete the group links tied to that contact, not the actual group entities. The same applies when deleting a group.
编辑:尽管以上内容通常适用,但实际问题是由两个引起的。对多
关系( User->联系人
和 User-> Group
)中未提及的问题,默认情况下级联删除
将打开
。这会导致经典的多个级联路径问题-删除 User
记录时,链接表 GroupContact
记录可以由 User-> Contact-> GroupContact
或 User-> Group-> GroupContact
,因此是多删除路径。
While the above applies in general, it turns out that the actual problem is caused by the two one-to-many
relationships (User->Contact
and User->Group
) not mentioned in the question which by default has cascade delete
turned on
. Which leads to a classical multiple cascade paths issue - when deleting a User
record, the link table GroupContact
records can be deleted by either User->Contact->GroupContact
or User->Group->GroupContact
, hence is a multiple delete path.
因此,您必须至少打开 User-> Contact
,中的一个用户->组
或联系人<->组
关系级联删除
。不幸的是,这将导致您的维护问题,因为您将无法简单地删除所涉及的实体之一。由于我猜您不是经常删除用户,因此建议您关闭 User-> Group
关系级联删除,然后手动删除相关的 User.Group
,然后删除 User
。或在数据库内的 User
表上放置一个删除触发器。
So you have to turn at least one of the User->Contact
, User->Group
or Contact<->Group
relationship cascade delete
off. Unfortunately that will cause you maintenance problems because you'll not be able to simple the delete one of the entities involved. Since I guess you are not deleting users so often, I would suggest you to turn User->Group
relationship cascade delete off and manually delete the related User.Groups
before deleting a User
. Or put a delete trigger on the User
table inside the database.
这篇关于从多对多关系中仅删除一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!