的EntityFramework代码优先自引用的一对一(1:1)关系的映射 - 无法确定到底本金 [英] EntityFramework Code First self-referencing one to one (1:1) relationship mapping - Unable to determine the principal end
问题描述
我有这个类:
public class Message
{
public long Id { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
public virtual Message ParentMessage { get; set; }
public virtual Message ChildMessage { get; set; }
//...
}
使用的EntityFramework
代码首先
添加迁移
给我的消息:无法确定类型之间的关联的主要终点...
Using EntityFramework
Code First
Add-Migration
gives me the message: Unable to determine the principal end of an association between the types...
我不能使用 [必需]
属性,因为在这个线程的第一条消息就没有父母,在线程的最后一条消息将没有孩子......我怎么映射呢?
I can't use the [Required]
attribute, because the first message in this thread will have no parent, the last message in the thread will have no child... how do I map this?
我试过:
modelBuilder.Entity<Message>()
.HasOptional(x => x.ParentMessage);
modelBuilder.Entity<Message>()
.HasOptional(x => x.ChildMessage);
但没有奏效。
but that did not work.
推荐答案
我发现一些看起来像它可能是它,如果有人可以确认这是正确的:
I found something that looks like it could be it, if someone could verify that this is correct:
modelBuilder.Entity<SecureMessage>()
.HasOptional(x => x.ParentMessage)
.WithOptionalDependent(x => x.ChildMessage);
于是经过一番认真的测试,这确实似乎是解决方案。
So after some serious testing, this does indeed seem to be the solution.
这篇关于的EntityFramework代码优先自引用的一对一(1:1)关系的映射 - 无法确定到底本金的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!