4

我得到了 ET 4.1 生成的用户模型 - 我首先制作了数据库:

public partial class User
{
    public User()
    {
        this.Photos = new HashSet<Photo>();
        this.Posts = new HashSet<Post>();
        this.My_requests = new HashSet<User>();
        this.Requests = new HashSet<User>();
        this.FriendLeft = new HashSet<User>();
        this.FriendRight = new HashSet<User>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Project { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string House_number { get; set; }
    public string Apartment_number { get; set; }
    public string username { get; set; }
    public string Zip_code { get; set; }

    public virtual ICollection<Photo> Photos { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual ICollection<User> My_requests { get; set; }
    public virtual ICollection<User> Requests { get; set; }
    public virtual ICollection<User> FriendLeft { get; set; }
    public virtual ICollection<User> FriendRight { get; set; }
}

每个“用户”都可以邀请其他“用户”成为他的朋友。因此,当有人邀请您时,他会出现在“请求”中,当您邀请某人时,他会出现在“我的请求”中。该数据存储在“Friend_requests”表中,如下所示:

ProposerId(col1) RecipientId(col2)

并且两列都通过 UserId 与 User 表相关 - 它是多对多的。

我的问题是从“Friend_request”中删除行,因为 EF 没有为我提供该表的上下文,而是生成了之前显示的模型......

我尝试过这样的事情:

User User2 = GetUserInfo(UserId2);
context.Users.Where(u => u.Id.Equals(UserId1)).SingleOrDefault().Requests.Remove(User2);
context.SaveChanges();

但这当然是错误的,它给了我一个错误:

"Can't update EntitySet „Friends_Request”, because it has element DefiningQuery, and in element there is no element , wchich can handle this operation."

And my questions are:
How I can delete and how to add rows to table 'Friend_Requests'?

EDIT:

Table relations:
enter image description here

And in model generated by ET it's:
My_requests -> when User is Proposer
Requests -> when User is Recipient

Still the problem is how to add new "Friend requests" and how to delete them?

4

1 回答 1

0

Of course Slauma as a experienced EF user, was right from the beginning. Without keys on Friends table it won't work. Add/remove elements now works normally as i write before. Just use like normal List. Learn on your own mistakes :)

于 2012-09-25T18:01:10.277 回答