gpt4 book ai didi

c# - EntityFramework Code First自引用一对一(1 :1) relationship mapping - Unable to determine the principal end

转载 作者:太空狗 更新时间:2023-10-30 01:20:14 24 4
gpt4 key购买 nike

我有这门课:

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 Code First Add-Migration 给我消息:Unable to determine the principal end of an association between the类型...

我不能使用[Required] 属性,因为该线程中的第一条消息没有父消息,线程中的最后一条消息也没有子消息...我该如何映射这个?

我试过:

        modelBuilder.Entity<Message>()
.HasOptional(x => x.ParentMessage);

modelBuilder.Entity<Message>()
.HasOptional(x => x.ChildMessage);

但这没有用。

最佳答案

我发现了一些看起来可能是它的东西,如果有人可以验证这是正确的:

        modelBuilder.Entity<SecureMessage>()
.HasOptional(x => x.ParentMessage)
.WithOptionalDependent(x => x.ChildMessage);

所以经过一些认真的测试,这似乎确实是解决方案。

关于c# - EntityFramework Code First自引用一对一(1 :1) relationship mapping - Unable to determine the principal end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19825848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com