简体   繁体   English

首先使用实体​​框架代码删除关系

[英]Delete a relationship using Entity Framework Code First

I have created my own database model using Entity Framework Code First with the latest Entity Framework version. 我已经使用Entity Framework Code First和最新的Entity Framework版本创建了自己的数据库模型。

I have this class: 我有这个课:

[DataContract]
public class User
{
    [DataMember]
    public int UserId { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }

    [DataMember]
    public string City { get; set; }

    [DataMember]
    public string Country { get; set; }

    [DataMember]
    public string Email { get; set; }

    [DataMember]
    public string InterestIn { get; set; }


    [DataMember]
    public virtual ICollection<User> Friends { get; set; }

    [DataMember]
    public virtual ICollection<User> FromWhomIsFriend { get; set; }

    [DataMember]
    public virtual ICollection<Activity> WantsToDo { get; set; }

    [DataMember]
    public virtual ICollection<Message> MessagesSent { get; set; }

    [DataMember]
    public virtual ICollection<Message> MessagesReceived { get; set; }

    public override string ToString()
    {
        string output = string.Empty;

        output = String.Format("UserId: {0}, Name: {1}, Age: {2}, City: {3}, " +
            "Country: {4}, Email: {5}, InterestIn: {6}",
            UserId, Name, Age, City, Country, Email, InterestIn);

        return output;
    }
}

And its configuration class: 及其配置类:

class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        Property(d => d.Name).IsRequired();
        Property(d => d.Age).IsRequired();
        Property(d => d.City).IsRequired();
        Property(d => d.Country).IsRequired();
        Property(d => d.Email).IsRequired();
        Property(d => d.InterestIn).IsRequired();

        HasMany(d => d.MessagesSent).WithRequired(l => l.SentByUser).WillCascadeOnDelete(false);
        HasMany(d => d.MessagesReceived).WithRequired(l => l.SentToUser).WillCascadeOnDelete(false);

        HasMany(d => d.Friends).
            WithMany(d => d.FromWhomIsFriend).
            Map(c =>
                {
                    c.ToTable("UserFriends");
                    c.MapLeftKey("UserId");
                    c.MapRightKey("FriendId");
                });
        HasMany(d => d.WantsToDo).
            WithMany(a => a.Users).
            Map(t =>
                {
                    t.ToTable("UserActivities");
                    t.MapLeftKey("UserId");
                    t.MapRightKey("ActivityId");
                });
    }
}

To represent this many-to-many self relationship: 代表这种多对多的自我关系:

在此处输入图片说明

Now I have a problem: I don't know how to delete friend relationship. 现在我有一个问题:我不知道如何删除朋友关系。 With SQL I will do something like this: 使用SQL,我将执行以下操作:

DELETE FROM UserFriend
WHERE (userId = user1_id   AND friendId = friend1_id) OR
      (userId = friend1_id AND friendId = user1_id)

user1_id : is the user who wants to delete a friend relationship. user1_id :是要删除朋友关系的用户。 friend1_id : is the friend user1 wants to remove as friend. friend1_id :是user1要删除的朋友。

How can I do it? 我该怎么做?

只需从集合中删除朋友:

user.Friends.Remove(friendToRemove);

So, if I undestand correctly, if the user User1 is friends with User2, the relationship will either be in user1.Friends or in user1.FromWhomIsFriend . 因此,如果我没有正确理解,如果用户User1与User2是朋友,则该关系将位于user1.Friendsuser1.FromWhomIsFriend

You should check where the relationship is, and remove it accordingly. 您应该检查关系在哪里,并相应地将其删除。

 public void RemoveFriend(User user, User friend)
 {
   if (user1.Friends.Contains(friend));
      user1.Friends.Remove(friend);
   if (user1.FromWhomIsFriend.Contains(friend));
      user1.FromWhomIsFriend.Remove(friend);
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM