问题
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; }
}
and also:
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; }
}
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
回答1:
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.
EDIT: 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.
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.
回答2:
You need to think about how your data is structured. Why does each contact have to hold a list of all groups? By doing this, each contact in your system holds all information about each other contact, including himself.
This is SQL's way of telling you your data relationship doesn't make sense.
Why not rather have each group hold a list of all contacts in its group?
That's your first option, but if you REALLY need to make this work, probably you want to build a mapping table. Lots of examples of these around.
来源:https://stackoverflow.com/questions/36567974/deleting-only-one-entry-from-many-to-many-relationship