【发布时间】:2010-12-29 15:32:14
【问题描述】:
如果两个表都不包含主键,他们是否可以使用 Entity Framework Code-Only 来拥有一个具有来自两个表的字段的实体?
这是一个例子。
public class BlogPost
{
public int PostID { get; set; }
public String PostBody { get; set; }
public int UserID { get; set; }
public string Username { get; set; }
}
public class User
{
public int UserID { get; set; }
public String Username { get; set; }
}
public class BlogPostConfiguration : EntityConfiguration<BlogPost>
{
public BlogPostConfiguration()
{
HasKey(b => b.PostID);
}
}
public class UserConfiguration : EntityConfiguration<User>
{
public UserConfiguration()
{
HasKey(b => b.UserID);
}
}
我希望将 BlogPost 对象的用户名属性映射到用户表的用户名列。我可以使用设计器使用外键进行映射,但我不确定如何使用“仅代码”进行映射。我尝试在我的配置对象中使用两个 MapHierarchy 语句,但它看起来只有在两个表都使用相同的主键时才有效。
【问题讨论】:
-
你看过这个来自 pdc09 的关于 EF 的microsoftpdc.com/Sessions/FT10 演示吗?如果没有 - 看看它。
-
感谢 Alexander,该视频很棒,但不幸的是它没有解决实体拆分问题。强烈推荐任何进入 EF 的人观看。
标签: .net entity-framework entity-framework-4 code-first