Fluent API 中的父子关系
好吧,这变得很荒谬,因为事实证明这比它应有的要困难得多。
如果我使用没有 FluentAPI 映射的原始代码,则有一个未使用的 ParentID 字段,并且使用一个名为 Node_ID 的新字段。
public class Node {
public long ID { get; private set; }
public long ParentID { get; set; }
public ICollection<Node> Children { get; set; }
}
以下是我的各种尝试:
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.HasForeignKey(h => h.ParentID);
}
DbUpdateException:无法确定相关操作的有效顺序。由于外键约束、模型要求或存储生成的值,可能会存在依赖关系。
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.Map(m => m.MapKey("ParentID"));
}
MetadataException:指定的架构无效。错误: (82,6):错误0019:类型中的每个属性名称必须是唯一的。属性名称“ParentID”已定义。
[ForeignKey("ParentID")]
public ICollection<Node> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
}
DbUpdateException:无法确定相关操作的有效顺序。由于外键约束、模型要求或存储生成的值,可能会存在依赖关系。
更新
使用上面我第一次尝试代码中的 Fluent API 代码(.HasForeignKey),并通过使 ParentID 可为空(public long?ParentID),我已经成功映射了数据库。有什么办法可以做到这一点而不使 FK 可为空吗?当没有父对象存在时,我希望密钥为 0。如果没有,哦,好吧,我会处理。
Okay, this is getting ridiculous as this is turning out to be much more difficult than it has any right to be.
If I use my original code with no FluentAPI mapping, I have a ParentID field which is not used, and a new field called Node_ID is used.
public class Node {
public long ID { get; private set; }
public long ParentID { get; set; }
public ICollection<Node> Children { get; set; }
}
Here are my various attempts:
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.HasForeignKey(h => h.ParentID);
}
DbUpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
.Map(m => m.MapKey("ParentID"));
}
MetadataException: Schema specified is not valid. Errors:
(82,6) : error 0019: Each property name in a type must be unique. Property name 'ParentID' was already defined.
[ForeignKey("ParentID")]
public ICollection<Node> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<Node>()
.HasMany<Node>(h => h.Children)
.WithOptional()
}
DbUpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Update
Using the Fluent API code from my first attempt code above (.HasForeignKey), and by making ParentID nullable (public long? ParentID), I have gotten the database to successfully map. Is there any way to do this without making the FK nullable? I would like the key to be 0 when no parent exists. If not, oh well, I will deal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,没有办法避免可为空的
ParentId
- 您告诉 EF 父项是可选的(必须是可选的,否则您将无法使用该表),因此相关的 FK 属性必须可为空。No there is no way to avoid nullable
ParentId
- you told EF that parent is optional (it must be otherwise you will not be able to use the table) and because of that related FK property must be nullable.