使用复合PK在表中设置外键关系

时间:2016-10-28 00:14:54

标签: c# sql sql-server entity-framework

我的数据库中有以下表格:

Public class Student
{
  [Key]
  string Id {get; set;}
  string Department {get; set;}

  #region Navigation properties 
  public virtual ICollection<Courses> Courses{get; set;} ----> Parent to child
  #endregion
}

Public class Courses
{
  [Key]
  [Column(Order = 1)]
  string Name{get; set;}
  [Key]
  [Column(Order = 2)]
  string StudentId {get; set;}
  string MaxCredits

  #region Navigation Properties
  [ForeignKey("StudentId")]
  public virtual Student Student {get; set;}  ----> Child to parent 
 }

在Courses表中,课程名称不是唯一的,因此需要复合主键。因此,根据上面的定义,我的子表中有一个外键,它是同一个表中复合主键的一部分。这种关系有效吗?

0 个答案:

没有答案
相关问题