EF核心多对多发行双重类型

时间:2018-12-18 21:55:42

标签: c# many-to-many entity-framework-core

我试图在EF Core 2.2中设置多对多附加属性。阅读后,看来我需要使加入表成为一等公民。我已经做到了,但是它生成的模式并不完全正确。它添加了一个不需要的阴影属性“ UserId1”。

这是域模型:

User.cs

 public class User : IdentityUser<long> {

        public string FirstName { get; set; }

        public string LastName { get; set; }
        public IList<UserJobRecommendations> RecommendedJobs { get; protected set; 
}

Job.cs

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

UserJobRecommendations.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个属性。 JobIdUserIdRecommendedById

  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 个答案:

答案 0 :(得分:1)

问题出在您的Many-to-Many实体配置中。您在Many-to-ManyUser之间进行的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();

}

现在,迁移一切都将按预期生成!