我正在使用具有简单设计的 Entity Framework 4.3:
public class Post
{
[Key]
public int ID { get; set; }
public string Text { get; set; }
public HashSet<PostTag> Tags { get; set; }
}
public class PostTag
{
public Post Post { get; set; }
[Key, Column(Order=0)]
[ForeignKey("Post")]
public int PostID { get; set; }
[Key, Column(Order=1)]
[MaxLength(50)]
public string Tag { get; set; }
}
所以你可以看到一个 Post 有多个标签,这些标签使用 PostID 和 Tag 的复合主键。
当我跑步时,update-database
我得到:
Cannot define PRIMARY KEY constraint on nullable column in table 'PostTag'.
我尝试将 [Required] 属性应用于 Post 和 PostID。我尝试将 ForeignKey 属性放在 FK 关系的另一端。我已经尝试将 InverseProperty 属性应用于 Post (它将错误更改为只是一个模糊的 NullReferenceException - 这似乎是一个错误)。