简体   繁体   English

非静态外键 EF 核心

[英]Non-static foreign key EF core

I am working on a messenger API, and it goning be something like discord which someone sends a friend request to another user, and if the user accepted, they start messaging.我正在开发一个信使 API,它会像不和谐一样有人向另一个用户发送好友请求,如果用户接受,他们就会开始消息传递。 as you know each FriendRequest has two contacts, sender and receiver, there are two ways to implement this relationship, one way is to define a many-to-many relationship between FriendRequest and contact and limit the FriendRequest 's contacts to two.如您所知,每个FriendRequest有两个联系人,发送者和接收者,有两种方法可以实现这种关系,一种方法是在FriendRequest和联系人之间定义多对多关系,并将FriendRequest的联系人限制为两个。 the other way is to define two properties in FriendRequest , SenderContact and ReceiverContact .另一种方法是在FriendRequest中定义两个属性, SenderContactReceiverContact

I chose the second way which each FriendRequest should have two foreign keys with two contacts.我选择了第二种方式,每个FriendRequest应该有两个外键和两个联系人。 But EF added another foreign key automatically.但是 EF 自动添加了另一个外键。 I just want to know two things我只想知道两件事
1: Is the second way that I chose a good way? 1:我选择的第二种方式是好方式吗? Is there any better way to implement this situation?有没有更好的方法来实现这种情况?
2: Can I prevent EF from creating the ContactId column? 2:我可以阻止 EF 创建ContactId列吗?

OnModelCreating in MyConext: MyConext 中的 OnModelCreating:

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<Contact>().HasMany<FriendRequest>().WithOne(f => f.FromContact);
    builder.Entity<Contact>().HasMany<FriendRequest>().WithOne(f => f.ToContact);
    base.OnModelCreating(builder);
}

FriendRequest:好友请求:

public class FriendRequest
{
    public int Id { get; set; }
    public Contact FromContact { get; set; }
    public Contact ToContact { get; set; }
    public string Text { get; set; }
}

Contact:接触:

public class Contact
{
    [Key]
    public int Id { get; set; }
    public int UserId { get; set; }
    public string Username { get; set; }
    public ICollection<Chat> Chats { get; set; }
    public ICollection<FriendRequest> FriendRequests { get; set; }
    public ICollection<Contact> Friends { get; set; }
}

我的数据库图

Your approach seems okay to me, but you have a configuration error.你的方法对我来说似乎没问题,但你有一个配置错误。 Your contact object should be您的联系对象应该是

public class Contact
{
    [Key]
    public int Id { get; set; }
    public int UserId { get; set; }
    public string Username { get; set; }
    public ICollection<Chat> Chats { get; set; }
    public ICollection<FriendRequest> SentFriendRequests { get; set; }
    public ICollection<FriendRequest> ReceivedFriendRequests { get; set; }
    public ICollection<Contact> Friends { get; set; }
}

FriendRequest can stay the same, tho it is recommended to define a foreign key FriendRequest 可以保持不变,但建议定义一个外键

public class FriendRequest
{
    public int Id { get; set; }
    public Contact FromContact { get; set; }
    public Contact ToContact { get; set; }
    public string Text { get; set; }
}

And the configuration和配置

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Contact>()
          .HasMany<FriendRequest>(c => c.SentFriendRequests)
          .WithOne(f => f.FromContact);

        builder.Entity<Contact>()
          .HasMany<FriendRequest>(c => c.ReceivedFriendRequests)
          .WithOne(f => f.ToContact);

        base.OnModelCreating(builder);
    }

I always take a second look at the MS documentation when configuring relations, I recommend you do that.在配置关系时,我总是会再次查看MS 文档,我建议您这样做。

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

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