Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
Entity framework 如何使用fluent api定义复杂的(1到0 | n)实体框架关系?_Entity Framework - Fatal编程技术网

Entity framework 如何使用fluent api定义复杂的(1到0 | n)实体框架关系?

Entity framework 如何使用fluent api定义复杂的(1到0 | n)实体框架关系?,entity-framework,Entity Framework,我有以下(简化)课程: 这里的意图是有人和账户。帐户不能没有成员而存在,并且成员始终是个人。但是,一个人可以不作为帐户的成员而存在 我的想法是希望能够查询以下内容: 给定一个特定的帐号,我想找到该帐号上的所有人 给定名/姓组合,我希望找到所有姓名匹配的人以及他们可能是其成员的任何帐户 我现在所拥有的似乎不起作用。我得到了一个循环参考错误,但我不太确定哪里出了错 谢谢你能提供的任何帮助 您正在定义两个独立的关系,而不是一个双向关系。试试这个: //In Accounts configuration

我有以下(简化)课程:

这里的意图是有人和账户。帐户不能没有成员而存在,并且成员始终是个人。但是,一个人可以不作为帐户的成员而存在

我的想法是希望能够查询以下内容:

  • 给定一个特定的帐号,我想找到该帐号上的所有人
  • 给定名/姓组合,我希望找到所有姓名匹配的人以及他们可能是其成员的任何帐户
  • 我现在所拥有的似乎不起作用。我得到了一个循环参考错误,但我不太确定哪里出了错


    谢谢你能提供的任何帮助

    您正在定义两个独立的关系,而不是一个双向关系。试试这个:

    //In Accounts configuration class
    ToTable("missacct");
    HasKey(a => a.Id);
    
    //In Members Configuration class
    ToTable("members");
    HasKey(a => new { a.AccountNumber, a.PersonNumber});
    HasRequired(m => m.Person).WithMany(p => p.Memberships).HasForeignKey(p => p.PersonNumber);
    HasRequired(m => m.Account).WithMany().HasForeignKey(a => a.AccountNumber);
    
    //In Persons Configuration Class
    ToTable("person");
    HasKey(p => p.Id);
    HasMany(p => p.Memberships).WithOptional(m => m.Person).HasForeignKey(p => p.PersonNumber);
    
    没有成员的帐户无法存在


    您必须在应用程序逻辑中强制执行此部分。在关系级别,您只能说成员必须拥有帐户。

    我看到的一件事是,我可能也应该将
    ICollection成员添加到Accounts类中。谢谢。WithMany(p=>p.Memberships)是一个帮助。我还将Person配置类中的WithOptional(m=>m.Person)更改为WithRequired(m=>m.Person),因为成员必须绑定到Person。
    
    //In Accounts configuration class
    ToTable("missacct");
    HasKey(a => a.Id);
    
    //In Members Configuration class
    ToTable("members");
    HasKey(a => new { a.AccountNumber, a.PersonNumber});
    HasRequired(a => a.Person).WithMany().HasForeignKey(p => p.PersonNumber);
    HasRequired(a => a.Account).WithMany().HasForeignKey(a => a.AccountNumber);
    
    //In Persons Configuration Class
    ToTable("person");
    HasKey(p => p.Id);
    HasMany(p => p.Memberships).WithOptional().HasForeignKey(p => p.PersonNumber);
    
    //In Accounts configuration class
    ToTable("missacct");
    HasKey(a => a.Id);
    
    //In Members Configuration class
    ToTable("members");
    HasKey(a => new { a.AccountNumber, a.PersonNumber});
    HasRequired(m => m.Person).WithMany(p => p.Memberships).HasForeignKey(p => p.PersonNumber);
    HasRequired(m => m.Account).WithMany().HasForeignKey(a => a.AccountNumber);
    
    //In Persons Configuration Class
    ToTable("person");
    HasKey(p => p.Id);
    HasMany(p => p.Memberships).WithOptional(m => m.Person).HasForeignKey(p => p.PersonNumber);