ef6使用网桥查询多对多

时间:2019-02-05 21:41:19

标签: linq entity-framework-6

如何查询这种多对多关系?我从ACCOUNT开始,并想返回与其关联的ExecutingBroker.Firm。

我先从Account开始,然后再钻到MANAGER,然后到MAPPING_MANAGER,再到EXECUTINGBROKER。

这是我到目前为止的查询...

            var student = dbEF.Accounts        
                        .Where(x => x.AccountNumber == acctNum)
                        .Select(x => new DTOCrmDetails()
                        {
                            AccountNumber = x.AccountNumber,
                            AccountName = x.AccountName,
                            DateOpened = x.DateOpened,

                            CommissionId = x.CommissionId,
                            Commission = x.Commission,

                            ManagerID = x.ManagerID,
                            ManagerName = x.Manager.ManagerName,
                            Manager = x.Manager,

                            Employees = x.Manager.Employees,

                            WireInstructionsUSD = x.Manager.WireInstructionsUSDs

                        }).FirstOrDefault();  

enter image description here

下面是从ef从现有数据库中生成的代码。

    public partial class Manager
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Manager()
    {
        this.Accounts = new HashSet<Account>();
        this.Employees = new HashSet<Employee>();
        this.WireInstructionsUSDs = new HashSet<WireInstructionsUSD>();
        this.Mapping_ManagersExecutingBrokers = new HashSet<Mapping_ManagersExecutingBrokers>();
    }

    public int ManagerID { get; set; }
    public string ManagerName { get; set; }
    public string Strategy { get; set; }
    public string ManagerShortCode { get; set; }
    public Nullable<int> WireInstructionsUsdID { get; set; }
    public Nullable<int> WireInstructionsForeignID { get; set; }
    public string MEtradingPlatform { get; set; }
    public string EtradingCostResp { get; set; }
    public string NotesManager { get; set; }
    public bool MainStrategy { get; set; }
    public string PathPayments { get; set; }
    public string PathEtrading { get; set; }
    public string LEI { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Account> Accounts { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Employee> Employees { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<WireInstructionsUSD> WireInstructionsUSDs { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Mapping_ManagersExecutingBrokers> Mapping_ManagersExecutingBrokers { get; set; }
}

}

{
using System;
using System.Collections.Generic;

public partial class Mapping_ManagersExecutingBrokers
{
    public int Mapping_ManagersExecutingBrokersId { get; set; }
    public Nullable<int> ManagerID { get; set; }
    public Nullable<int> ExecutingBrokersId { get; set; }

    public virtual ExecutingBroker ExecutingBroker { get; set; }
    public virtual Manager Manager { get; set; }
}

}

    public partial class ExecutingBroker
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public ExecutingBroker()
    {
        this.Mapping_ManagersExecutingBrokers = new HashSet<Mapping_ManagersExecutingBrokers>();
    }

    public int ExecutingBrokersId { get; set; }
    public string Firm { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Mapping_ManagersExecutingBrokers> Mapping_ManagersExecutingBrokers { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您必须经过Mapping_ManagersExecutingBrokers的建模,因为它已经过建模。 请记住,由于这是一个多对多关系,因此您拥有许多公司。

.Select(account => new { Firms = account.Manager.Mapping_ManagersExecutingBrokers
                                .Select(meb => meb.ExecutingBroker.Firm) });