EF FluentAPI 0..1 到 * 映射
如何在 EF 4.0 FluentAPI CTP5 中将 0..1 映射到 * 关系? 我不断收到此错误
因为从属角色中的所有属性都是不可为空的, 主要角色的重数必须为“1”。
我不知道如何准确地修复它。
我的代码看起来像这样
public class Child{
public int pID { get; set; }
public Parent Parent_Object{ get; set; }
public int Parent{ get; set; }
public Child() {
}
}
public class Parent {
public int pID { get; set; }
public List<Child> Children { get; set; }
public Parent () {
}
}
对于映射,代码看起来像这样
modelBuilder.Entity<Child>().HasKey(c=> c.pID);
modelBuilder.Entity<Parent>().HasKey(c=> c.pID);
modelBuilder.Entity<Child>().HasOptional(c=> c.Parent_Object)
.WithMany(p => p.Children)
.HasForeignKey(p => p.Parent);
也可以只
public Parent Parent{ get; set; }
代替
public Parent Parent_Object{ get; set; }
public int Parent{ get; set; }
在数据库中,FKfield 被命名为“Parent”而不是“ParentpID”。在这种情况下,映射应该是什么样的?
How do I map a 0..1 to * relation in EF 4.0 FluentAPI CTP5?
I keep geting this error
Because all of the properties in the Dependent Role are non-nullable,
multiplicity of the Principal Role must be '1'.
And I do not know how exactly to fix it..
My code looks like this
public class Child{
public int pID { get; set; }
public Parent Parent_Object{ get; set; }
public int Parent{ get; set; }
public Child() {
}
}
public class Parent {
public int pID { get; set; }
public List<Child> Children { get; set; }
public Parent () {
}
}
For mapping the code looks like this
modelBuilder.Entity<Child>().HasKey(c=> c.pID);
modelBuilder.Entity<Parent>().HasKey(c=> c.pID);
modelBuilder.Entity<Child>().HasOptional(c=> c.Parent_Object)
.WithMany(p => p.Children)
.HasForeignKey(p => p.Parent);
Also is it possible to have only
public Parent Parent{ get; set; }
instead of
public Parent Parent_Object{ get; set; }
public int Parent{ get; set; }
In the database the FKfield is named "Parent" not "ParentpID". In this case how should the mapping look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简单地从映射中删除外键列,一切都会正常工作:
作为替代方案,您可以使用可空 int Parent 属性,如下所示:
在这种情况下,您的初始代码也将是正确的。
You can simply remove Foreign Key Column from the mapping and everything will work fine:
As an alternative, you can use the nullable int Parent property, like this:
In this case your initial code will be correct as well.