代码之家  ›  专栏  ›  技术社区  ›  Chris Kooken

英孚核心多对多问题的双重类型

  •  1
  • Chris Kooken  · 技术社区  · 6 年前

    我正在尝试在ef core 2.2中设置多对多的附加属性。看完后,我需要让我的加入表成为一等公民。我已经这样做了,但是它生成的模式并不完全正确。它添加了一个不需要的影子属性“userid1”。

    以下是域模型:

    用户代码

     public class User : IdentityUser<long> {
    
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
            public IList<UserJobRecommendations> RecommendedJobs { get; protected set; 
    }
    

    乔布斯

    public class Job : BaseEntity<Job> {
        public long Id { get; protected set; }        
        public string Name {get ; protected set;}
        public IList<UserJobRecommendations> RecommendedTo { get; protected set; }
    }
    

    用户作业建议.cs

    public class UserJobRecommendations {
        public Job Job { get; protected set; }
    
        public long JobId { get; protected set; }
        public User User { get; protected set; }
    
        public long UserId { get; protected set; }
    
        public long RecommendedById { get; protected set; }
        public User RecommendedBy { get; protected set; }
        }
    

    最后,这里是我的背景:

    public class MyContext : IdentityDbContext<User, IdentityRole<long>, long> {
            public DbSet<Job> Jobs { get; set; }    
            public DbSet<UserJobRecommendations> UserJobRecommendations { get; set; }
    
    
            protected override void OnModelCreating(ModelBuilder modelBuilder) {            
    
                modelBuilder.Entity<UserJobRecommendations>().HasKey(k => new { k.UserId, k.JobId });
    
                modelBuilder.Entity<UserJobRecommendations>().HasOne(x => x.User).WithMany(x=>x.RecommendedJobs);
                modelBuilder.Entity<UserJobRecommendations>().HasOne(x => x.RecommendedBy);
    
                modelBuilder.Entity<User>().HasMany(x => x.RecommendedJobs);
                modelBuilder.Entity<Job>().HasMany(x => x.RecommendedTo);
    
    
    
                modelBuilder.Seed();
                base.OnModelCreating(modelBuilder);
            }
    

    下面是它为此创建的模式示例。我不想要 userId1 财产。该表应具有3个属性。 JobId ,请 UserId , RecommendedById .

      migrationBuilder.CreateTable(
                    name: "UserJobRecommendations",
                    columns: table => new
                    {
                        JobId = table.Column<long>(nullable: false),
                        UserId = table.Column<long>(nullable: false),
                        UserId1 = table.Column<long>(nullable: true),
                        RecommendedById = table.Column<long>(nullable: false)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_UserJobRecommendations", x => new { x.UserId, x.JobId });
                        table.ForeignKey(
                            name: "FK_UserJobRecommendations_Jobs_JobId",
                            column: x => x.JobId,
                            principalTable: "Jobs",
                            principalColumn: "Id",
                            onDelete: ReferentialAction.Cascade);
                        table.ForeignKey(
                            name: "FK_UserJobRecommendations_AspNetUsers_RecommendedById",
                            column: x => x.RecommendedById,
                            principalTable: "AspNetUsers",
                            principalColumn: "Id",
                            onDelete: ReferentialAction.Cascade);
                        table.ForeignKey(
                            name: "FK_UserJobRecommendations_AspNetUsers_UserId1",
                            column: x => x.UserId1,
                            principalTable: "AspNetUsers",
                            principalColumn: "Id",
                            onDelete: ReferentialAction.Restrict);
                    });
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   TanvirArjel    6 年前

    问题出在你身上 Many-to-Many 实体配置。你的 多对多 配置介于 User Job 书写不正确。

    编写实体配置如下:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {   
        base.OnModelCreating(modelBuilder);
    
        // Many-to-Many configuration between User and Job 
        modelBuilder.Entity<UserJobRecommendations>().HasKey(ujr => new { ujr.UserId, ujr.JobId });
        modelBuilder.Entity<UserJobRecommendations>().HasOne(ujr => ujr.User).WithMany(u=>u.RecommendedJobs)
                                                     .HasForeignKey(ujr => ujr.UserId).OnDelete(DeleteBehavior.Restrict);
        modelBuilder.Entity<UserJobRecommendations>().HasOne(ujr => ujr.Job).WithMany(j=>j.RecommendedJobs)
                                                     .HasForeignKey(ujr => ujr.JobId).OnDelete(DeleteBehavior.Restrict);
    
        // RecommendedBy ForeignKey Configuraiton
        modelBuilder.Entity<UserJobRecommendations>().HasOne(ujr => ujr.RecommendedBy).WithMany().HasForeignKey(ujr => ujr.RecommendedById);
    
        modelBuilder.Seed();
    
    }
    

    现在,在迁移过程中,一切都应该按预期生成!