Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在实体框架中创建自引用分层表_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 在实体框架中创建自引用分层表

C# 在实体框架中创建自引用分层表,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,背景 我有一个类看起来或多或少像这样: public class MyClass { [Id] public long Id { get; set; } public string MyProperty { get; set; } public bool MyBoolean { get; set; } public string AnotherProperty { get; set; } public MyClass ChildOne {

背景

我有一个类看起来或多或少像这样:

public class MyClass 
{
    [Id]
    public long Id { get; set; }

    public string MyProperty { get; set; }
    public bool MyBoolean { get; set; } 
    public string AnotherProperty { get; set; }

    public MyClass ChildOne { get; set; }
    public MyClass ChildTwo { get; set; }
}
modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildOne).WithOptionalDependent();
modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildTwo).WithOptionalDependent();
我需要使用存储过程来加载一组记录,但只要结构本身正确就可以

对于
MyClass
的任何实例,一个子项或两个子项都可以为null。
MyClass
的任何实例都可以在父类中使用,但是子类本身不需要知道这种关系,并且子类可以被任意数量的父类使用

问题

使用此结构,我在创建新迁移时遇到以下错误:

无法确定服务器之间关联的主体端 类型“MyClass”和“MyClass”。这个协会的主要目的 必须使用关系fluent API显式配置 或数据注释

这个错误是有道理的——当给定一个对象的结构,它本身有一个外键时,EF很难确定主端,我并不感到奇怪。不过,我不知道如何解决这个问题

我尝试了一些不同的Fluent映射:

modelBuilder.Entity<MyClass>().HasOptional(x => x.ChildOne).WithOptionalPrincipal(x => x.ChildOne);
modelBuilder.Entity<MyClass>().HasOptional(x => x.ChildOne).WithOptionalDependent(x => x.ChildOne);
modelBuilder.Entity<MyClass>().HasOptional(x => x.ChildOne);
modelBuilder.Entity();
modelBuilder.Entity();
modelBuilder.Entity().has可选(x=>x.ChildOne);
(注意:我没有同时尝试这些,我一次尝试一个,并为ChildTwo复制了一个。)

我可以通过向MyClass添加
ChildThree
属性来实现迁移,但这没有意义,也不是一个有用的属性;它只是在表上创建另一个外键,但在我的模型中不需要

因此,总而言之:

  • 我如何让这个结构按我想要的方式工作?我认为秘密在于一些流畅的伏都教地图,但我对这个图库非常陌生,我不知道如何让它发挥作用

  • 为什么添加第三个(不需要、不需要的)属性可以修复所有问题并允许迁移到scaffold


  • 你的流畅映射是完全错误的

    你应该这样做:

    public class MyClass 
    {
        [Id]
        public long Id { get; set; }
    
        public string MyProperty { get; set; }
        public bool MyBoolean { get; set; } 
        public string AnotherProperty { get; set; }
    
        public MyClass ChildOne { get; set; }
        public MyClass ChildTwo { get; set; }
    }
    
    modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildOne).WithOptionalDependent();
    modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildTwo).WithOptionalDependent();
    
    modelBuilder.Entity();
    modelBuilder.Entity();
    
    您的流畅映射完全错误

    你应该这样做:

    public class MyClass 
    {
        [Id]
        public long Id { get; set; }
    
        public string MyProperty { get; set; }
        public bool MyBoolean { get; set; } 
        public string AnotherProperty { get; set; }
    
        public MyClass ChildOne { get; set; }
        public MyClass ChildTwo { get; set; }
    }
    
    modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildOne).WithOptionalDependent();
    modelBuilder.Entity<MyClass>().HasOptional(p => p.ChildTwo).WithOptionalDependent();
    
    modelBuilder.Entity();
    modelBuilder.Entity();