悟小小空

EF CORE创建组合主键

电脑版发表于:2020/12/22 11:42

使用modelBuilder.Entity<Type>().HasKey指定多个字段即可

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Course>().ToTable("Course");
    modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
    modelBuilder.Entity<Student>().ToTable("Student");
    modelBuilder.Entity<Department>().ToTable("Department");
    modelBuilder.Entity<Instructor>().ToTable("Instructor");
    modelBuilder.Entity<OfficeAssignment>().ToTable("OfficeAssignment");
    modelBuilder.Entity<CourseAssignment>().ToTable("CourseAssignment");

    //组合主键
    modelBuilder.Entity<CourseAssignment>()
        .HasKey(c => new { c.CourseID, c.InstructorID });
}


关于TNBLOG
TNBLOG,技术分享
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢