实体框架:外键作为复合键的一部分



我们有要求,我们需要在另一个表中使用外键作为复合键的一部分。 例如

Public Primary_Class{
public int pkey{get; set;}
public string Misc{get; set;}
}

Public dependent_Class{
*public int dkey {get; set;} /*Column 1 of Primary key*/
public int pkey{get; set;}  /*Cloumn 2 of PK as well as FK to Primay_Class*/
pubic string data{get; set;}
}

能否帮助我在实体框架 6.0 中实现这一目标

看来你曾经尝试过,但我仍然在回答。

在实体框架中,可以使用实体框架代码优先数据批注来实现此目的。

这是完整的解决方案:

public class Primary_Class
{
[Key]
public int pkey { get; set; }
public string Misc { get; set; }
}
public class dependent_Class
{
[Key]
public int dkey { get; set; } //*column 1 of Primary key*/
[ForeignKey("Primary_Class")]
[Column(Order = 1)]
public int pkey { get; set; }  //*Cloumn 2 of PK as well as FK to Primay_Class*
public string data{get; set;}
}

最新更新