Configuring a Composite primary key or Foreign Key 复合主键外键的写法

If the primary key on the Department type consisted of DepartmentID and Name properties, you would configure the primary key for the Department and the foreign key on the Course types as follows:
如果 Department 类型的主键由 DepartmentID 和 Name 属性组成,那么您将配置 Department 的主键和 Course 类型的外键,如下所示:

// Composite primary key 
modelBuilder.Entity<Department>() 
.HasKey(d => new { d.DepartmentID, d.Name }); 
 
// Composite foreign key 
modelBuilder.Entity<Course>()  
    .HasRequired(c => c.Department)  
    .WithMany(d => d.Courses) 
    .HasForeignKey(d => new { d.DepartmentID, d.DepartmentName });
 
参考:http://msdn.microsoft.com/en-us/data/jj591620.aspx

来自:https://blog.csdn.net/hejisan/article/details/35783235

原文地址:https://www.cnblogs.com/djd66/p/14849457.html