简体   繁体   English

EF Core 是一对多还是多对多? 用户<-->朋友

[英]EF Core one-to-many or many-to-many ? users<-->friends

I'm going to implement a user-friends module in my project.我将在我的项目中实现一个用户朋友模块。

Let's say I have 2 simple classes:假设我有 2 个简单的类:

public class User
{
    int Id { get; set; }
    string Name { get; set; }
}

and FriendShip class (which is a relation between user)和 FriendShip 类(这是用户之间的关系)

   public class FriendShip
   {
       int User1Id { get; set; }
       int User2Id { get; set; }
       int Status  { get; set; }
   }

Status is Enum状态为枚举

Logic for these 2 classes:这两个类的逻辑:

  • when first user invite second user to his friendship-list, there should be 2 objects created in the database当第一个用户邀请第二个用户加入他的好友列表时,数据库中应该创建了 2 个对象

    user1Id: 1, user2Id: 2, Status: 1, (where 1 means 'invited')

    and

    user1Id: 2, user2Id: 1, Status: 0, (where 0 means 'nonaccepted')
  • when second user accepts, these 2 status will be updated by value 3 (where means 'friendship' or something like that - but forget about logic now.当第二个用户接受时,这两个状态将更新为值 3(其中表示“友谊”或类似的东西 - 但现在忘记逻辑。

I don't know how can i create this database structure from code (in C#)我不知道如何从代码(在 C# 中)创建这个数据库结构

it will be one-to-many, like one user can have many relationships to other-users?它将是一对多的,就像一个用户可以与其他用户有很多关系一样?

thee private key must be 2 fk: user1, user2你的私钥必须是 2 fk: user1, user2

the problem is, that these 2 foreign keys goes to one table (Users)问题是,这 2 个外键转到一张表(用户)

I tried something like this:我试过这样的事情:

    modelBuilder.Entity<User>()
        .HasMany(x=>x.UsersFriendships)
        .WithOne(x=>x.User2)

    modelBuilder.Entity<UsersFriendship>()
        .HasMany(x=> x.Users)
        .WithOne(x=>x.UsersFriendships)

but it does not have sense, I can't do the second statement但它没有意义,我不能做第二个陈述

.WithOne(x => x.userFriendships)

The simplest way is to type in model builder:最简单的方法是输入模型构建器:

 modelBuilder.Entity<UsersFriendship>()
        .HasKey(x => new { x.User1Id, x.User2Id });

and the class UserFriendship will be like this: UserFriendship 类将是这样的:

   public class UsersFriendship
    {

        [ForeignKey("User1Id")]
        public User User1 { get; set; }

        [ForeignKey("User2Id")]
        public User User2 { get; set; }

        public int User1Id { get; set; }
        public int User2Id { get; set; }
        public RelationStatus RelationStatus { get; set; }
    }

but from now, i cannot naviage from User entity to his friends (like User.Friendship)但从现在开始,我无法从用户实体导航到他的朋友(如 User.Friendship)

and when i add navigation property to User class:当我将导航属性添加到 User 类时:

public virtual List<UsersFriendship> UsersFriendships { get; set; }

the database will four fields: user1Id, user2Id, status, userId数据库会有四个字段:user1Id、user2Id、status、userId

the latest field comes from user class (UsersFriendships field) <-- and i dont want this field on my database!最新字段来自用户类(UsersFriendships 字段)<-- 我不想在我的数据库中使用这个字段!

You can find one example in the official docs: https://docs.microsoft.com/en-us/ef/core/modeling/relationships#many-to-many .您可以在官方文档中找到一个示例: https : //docs.microsoft.com/en-us/ef/core/modeling/relationships#many-to-many

I adjusted this example to reflect your case:我调整了这个例子来反映你的情况:

namespace foo
{
  public class User
  {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<UsersFriendship> UsersFriendships { get; set; }
  }

  public class Friendship
  {
      public User User1 { get; set; }
      public int User1Id { get; set; }
      public User User2 { get; set; }
      public int User2Id { get; set; }
      public int Status  { get; set; }
  }

  class MyContext : DbContext
  {
      public DbSet<Friendship> Friendships { get; set; }
      public DbSet<User> Users { get; set; }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity<Friendship>()
              .HasKey(fs => new { fs.User1, fs.User2, fs.Status });

          modelBuilder.Entity<Friendship>()
              .HasOne(fs => fs.User1)
              .WithMany(u => u.UsersFriendships)
              .HasForeignKey(fs => fs.User1);

          modelBuilder.Entity<Friendship>()
              .HasOne(fs => fs.User2)
              .WithMany(u => u.UsersFriendships)
              .HasForeignKey(fs => fs.User2);
      }
  }

the latest field comes from user class (UsersFriendships field) <-- and i dont want this field on my database!最新字段来自用户类(UsersFriendships 字段)<-- 我不想在我的数据库中使用这个字段!

When registering this as navigational property (see my code) then it will not be stored in db.将其注册为导航属性时(请参阅我的代码),它将不会存储在 db 中。

In other cases, if for some reason you don't want a property to be stored in db by ef core, mark the property with the [NotMapped] attribute ( namespace System.ComponentModel.DataAnnotations.Schema ).在其他情况下,如果由于某种原因您不希望 ef 核心将属性存储在 db 中,请使用[NotMapped]属性( namespace System.ComponentModel.DataAnnotations.Schema )标记该属性。

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

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