【发布时间】:2012-03-01 21:50:53
【问题描述】:
我正在使用具有简单设计的 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; }
}
所以你可以看到一个帖子有多个标签,这些标签使用 PostID 和标签的复合主键。
当我运行update-database 时,我得到:
Cannot define PRIMARY KEY constraint on nullable column in table 'PostTag'.
我尝试将 [Required] 属性应用于 Post 和 PostID。我尝试将 ForeignKey 属性放在 FK 关系的另一端。我已经尝试将 InverseProperty 属性应用于 Post(它将错误更改为只是一个模糊的 NullReferenceException - 这似乎是一个错误)。
【问题讨论】:
标签: foreign-key-relationship composite-primary-key entity-framework-4.3