EF4 CTP5 - 在没有对象引用的情况下映射外键?
我觉得这应该有一个简单的答案,但我找不到。
我有 2 个 POCO:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
}
请注意,两个 POCO 上都没有对象引用。使用 Code-First,如何让 EF4 CTP5 定义两个数据库表之间的关系?
(我知道这是一个不寻常的场景,但我正在探索代码优先的可能性和不可能性)
I feel like this should have a simple answer, but I can't find it.
I have 2 POCOs:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
}
Notice that there are no object references on either POCO. With Code-First, how do I make EF4 CTP5 define a relationship between the two database tables?
(I know this is an unusual scenario, but I am exploring what's possible and what's not with Code-First)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。如下所示,所有用于设置关联的流畅 API 方法都需要指定
Navigation Property
作为其参数。No, this is not possible. As you can see below, all of the fluent API methods for setting up associations require specifying the
Navigation Property
as their parameter.您不想使用对象引用有什么特殊原因吗?使用它们看起来非常优雅:
您仍然可以通过您的产品以
product.Category.Id
的形式访问类别 Id。Is there any particular reason you don't want to use object references? It looks very elegant to use them:
And you can still access the Category Id via your product as
product.Category.Id
.