新的代码优先方法和EF Core。遇到了如何为通过共同实体加入的3个实体实施解决方案的问题。
这是我的实体
public class Activity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string Name { get; set; }
public ICollection<RoleApplicationActivity> RoleActivities{ get; set;
}
public class Application
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string Name { get; set; }
public ICollection<RoleApplicationActivity> RoleActivities { get; set; }
}
public class Role
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<RoleApplicationActivity> ApplicationActivities { get; set; }
}
public class RoleApplicationActivity
{
public int RoleId { get; set; }
public Role Role { get; set; }
public int ApplicationId { get; set; }
public Application Application { get; set; }
public int ActivitId { get; set; }
public Activity Activity { get; set; }
}
我基本上想要实现以下表格映射
Role Application Activity
1 1 1
1 1 2
2 1 1
2 2 1
如果我的实体设计对于这种方法是正确的,你能帮助我吗?
这是我为三联接表创建密钥的代码。
modelBuilder.Entity<RoleApplicationActivity>()
.HasKey(ur => new { ur.RoleId, ur.ApplicationId, ur.ActivitId });
编辑:
看起来我找到了答案。我在sql server和Scaffold-DbContext中生成了我的设计,为我生成代码。
以下是它生成的代码。
public partial class Activity
{
public Activity()
{
RoleApplicationActivity = new HashSet<RoleApplicationActivity>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }
}
public partial class Application
{
public Application()
{
RoleApplicationActivity = new HashSet<RoleApplicationActivity>();
}
public string Name { get; set; }
public int Id { get; set; }
public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }
}
public partial class Role
{
public Role()
{
RoleApplicationActivity = new HashSet<RoleApplicationActivity>();
}
public string Name { get; set; }
public int Id { get; set; }
public ICollection<RoleApplicationActivity> RoleApplicationActivity { get; set; }
}
public partial class RoleApplicationActivity
{
public int Id { get; set; }
public int RoleId { get; set; }
public int ApplicationId { get; set; }
public int ActivityId { get; set; }
public Activity Activity { get; set; }
public Application Application { get; set; }
public Role Role { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Activity>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Name)
.IsRequired()
.HasColumnType("nchar(10)");
});
modelBuilder.Entity<Application>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Name)
.IsRequired()
.HasColumnType("nchar(10)");
});
modelBuilder.Entity<Role>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Name)
.IsRequired()
.HasColumnType("nchar(50)");
});
modelBuilder.Entity<RoleApplicationActivity>(entity =>
{
entity.HasOne(d => d.Activity)
.WithMany(p => p.RoleApplicationActivity)
.HasForeignKey(d => d.ActivityId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_RoleApplicationActivity_Activity");
entity.HasOne(d => d.Application)
.WithMany(p => p.RoleApplicationActivity)
.HasForeignKey(d => d.ApplicationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_RoleApplicationActivity_Application");
entity.HasOne(d => d.Role)
.WithMany(p => p.RoleApplicationActivity)
.HasForeignKey(d => d.RoleId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_RoleApplicationActivity_Role");
});
}