![](/img/trans.png)
[英]EF 6 Code-First Many to Many relationship with same entity on non-primary key
[英]How can I have the primary key of one entity be another entity in EF4 code-first?
假设我有一个Post
实体,并且我想存储所有对post对象的更改,所以我也有一个PostHistory
实体。 现在,我将历史定义如下:
public class PostHistory
{
public virtual Post Post { get; set; }
//... properties of the posthistory object
}
问题是我不知道如何使它与EF4 Code-First一起使用。 最初,模型构建失败,因为没有为PostHistory
定义主键。 因此,我称之为
modelBuilder.Entity<PostHistory>().HasKey(x => x.Post);
并且发生以下异常:
System.MissingMethodException: No parameterless constructor defined for this object..
那个例外不是在谈论PostHistory
对象没有无参数的构造函数,因为我添加了一个并且它没有帮助。
任何想法如何做到这一点?
public class Post {
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public int Id { get; set; }
public virtual ICollection<PostHistory> Histories { get; set; }
}
public class PostHistory {
[Key, DatabaseGenerated(DatabaseGenerationOption.Identity)]
public int Id { get; set; }
public int PostId { get; set; }
public virtual Post Post { get; set; }
//... properties of the posthistory object
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.