如何让一个实体的主键成为 EF4 代码优先中的另一个实体?
假设我有一个 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
对象没有无参数构造函数,因为我添加了一个,但它没有帮助。
有什么想法如何实现这一点?
Say I have a Post
entity, and I want to store all changes to post objects, so I also have a PostHistory
entity. Right now I would define the history as something like this:
public class PostHistory
{
public virtual Post Post { get; set; }
//... properties of the posthistory object
}
The problem is I can't figure out how to get this to work with EF4 Code-First. At first model building fails because there is no primary key defined for PostHistory
. So in my Context I call
modelBuilder.Entity<PostHistory>().HasKey(x => x.Post);
And the following exception occurs:
System.MissingMethodException: No parameterless constructor defined for this object..
That exception is not talking about the PostHistory
object not having a parameterless constructor, because I added one and it didn't help.
Any ideas how to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)