【发布时间】:2012-06-29 07:59:58
【问题描述】:
通过示例更容易展示——我使用代码优先来构建数据库。我有以下课程:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string AuthorName { get; set; }
public List<Post> Posts { get; set; }
public string BlogCode
{
get
{
return Title.Substring(0, 1) + ":" + AuthorName.Substring(0, 1);
}
}
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public virtual Blog Blog { get; set; }
}
我不明白为什么 Post 需要公共虚拟博客博客。它是否充当数据库中的外键以链接回博客?如果是这种情况,您似乎会使用博客 ID。
【问题讨论】:
-
如果它是 NHibernate 之类的东西,则需要
virtual是因为创建了一个动态代理,overrides 成员提供延迟加载功能。
标签: database asp.net-mvc-3 entity-framework ef-code-first