在 EF Code First 中生成外键时出现问题 - 无法确定关联的主体端
Problem generating foreign key in EF Code First - Unable to determine the principal end of an association
我正在尝试在包含以下 table 关系的新数据库上启用迁移:
其中 HolderID 可以为 NULL。 运行 enable-migrations 给出错误消息“无法确定关联的主体端”
我的2个类如下:
public class Competitor
{
[Key]
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
public virtual Championship Championship { get; set; }
}
public class Championship
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int? HolderID { get; set; }
[ForeignKey("HolderID")]
public virtual Competitor Holder { get; set; }
}
我不明白我需要如何更正我的模型以反映所需的数据库模式结构。如果可能的话,我更愿意使用注释而不是流利的 api。
提前致谢。
public virtual Championship Championship { get; set; }
需要
public virtual ICollection<Championship> Championships { get; set; }
我正在尝试在包含以下 table 关系的新数据库上启用迁移:
其中 HolderID 可以为 NULL。 运行 enable-migrations 给出错误消息“无法确定关联的主体端”
我的2个类如下:
public class Competitor
{
[Key]
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
public virtual Championship Championship { get; set; }
}
public class Championship
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public int? HolderID { get; set; }
[ForeignKey("HolderID")]
public virtual Competitor Holder { get; set; }
}
我不明白我需要如何更正我的模型以反映所需的数据库模式结构。如果可能的话,我更愿意使用注释而不是流利的 api。
提前致谢。
public virtual Championship Championship { get; set; }
需要
public virtual ICollection<Championship> Championships { get; set; }