我正在尝试在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);
});