如何实现与Entity Framework的多对多关系?

时间:2015-07-22 07:48:03

标签: c# entity-framework

我有两张桌子,有很多关系。我正在使用实体框架。

这些是我的模型类:

public partial class PE_Attivita
{
    public PE_Attivita()
    {
        this.ANAG_OperatoriMedici = new HashSet<ANAG_OperatoriMedici>();
    }

    public int ID { get; set; }
    public string Descrizione { get; set; }
    public Nullable<int> IDAttivitaPare { get; set; }
    public Nullable<int> CodiceCategoria { get; set; }
    public string NomeAttivita { get; set; }

    public virtual ICollection<ANAG_OperatoriMedici> ANAG_OperatoriMedici { get; set; }
}

public partial class ANAG_OperatoriMedici
{
    public ANAG_OperatoriMedici()
    {
        this.PE_Attivita = new HashSet<PE_Attivita>();
    }

    public string CodicePersoneFisiche { get; set; }
    public string Descrizione { get; set; }

    public virtual ANAG_PersoneFisiche ANAG_PersoneFisiche { get; set; }
    public virtual ICollection<PE_Attivita> PE_Attivita { get; set; }
}

在我的数据库中,有第三个表来实现多对多的关系。

现在我想从这个表中提取所有数据,所以我写了这段代码:

activityForDoctors = from ad in db_data.PE_Attivita
.SelectMany(c => c.ANAG_OperatoriMedici)
    select new ActivityDTO.activitysXDoctors
    {
        codiceFiscaleDottore = ad.CodicePersoneFisiche,
        IdAttivita = a.ID
    }

此代码有效,但我收到了重复数据,所以我想插入子句Group by,并且我已将代码更改为:

activityForDoctors = from ad in db_data.PE_Attivita
   .SelectMany(c => c.ANAG_OperatoriMedici).GroupBy(prop=>prop.CodicePersoneFisiche,prop=>a.ID)
    select new ActivityDTO.activitysXDoctors
    {
    codiceFiscaleDottore = ad.CodicePersoneFisiche,
    IdAttivita = a.ID
    }

但是我从编译器收到了这个错误:

  

'System.Linq.Igrouping'不包含0'CodicePersoneFisiche'的定义,并且没有可以找到接受类型'System.Linq.Igrouping'的第一个参数的扩展方法'CodicePersoneFisiche'(你是否错过了使用程序集)引用?)

修改

这是Model.edmx.diagram

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram DiagramId="2f7abfd0eb854868836eeaf6d91c93a4" Name="Diagram1" ZoomLevel="74">
        <EntityTypeShape EntityType="ModelOmniacareData.ANAG_OperatoriMedici" Width="1.5" PointX="18.25" PointY="15.5" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.ANAG_OperatoriMediciXAssistiti" Width="1.5" PointX="20.5" PointY="18.75" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.ANAG_PersoneFisiche" Width="1.5" PointX="16" PointY="24" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_Attivita" Width="1.5" PointX="13" PointY="10.5" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_AttivitaXIndici" Width="1.5" PointX="15.25" PointY="11" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_AttivitaXParametri" Width="1.5" PointX="18.25" PointY="7.625" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_CategoriaAttivita" Width="1.5" PointX="10.75" PointY="11.625" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_IndiciValutazione" Width="1.5" PointX="13" PointY="6.5" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_Parametri" Width="1.5" PointX="16" PointY="19.375" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_ParametriInputSchedulazioneAttivita" Width="1.5" PointX="22.75" PointY="11.25" IsExpanded="true" />
        <EntityTypeShape EntityType="ModelOmniacareData.PE_Schedulazione_Attivita" Width="1.5" PointX="20.5" PointY="10.375" IsExpanded="true" />
        <AssociationConnector Association="ModelOmniacareData.FK_ANAG_OperatoriMedici" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_OperatoreMedicoXAssistiti_OperatoreMedico" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_ParametriXValori" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK__MONITOR_D__IDDat__4B622666" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_Attivita" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_AttivitaCodiceCategoria" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_AttivitaXIndiciAttivita" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_AttivitaXParametriAttivita" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_AttivitaXIndici" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.FK_AttivitaXParametri" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.ANAG_AssociazioniXOperatoriMedici" ManuallyRouted="false" />
        <AssociationConnector Association="ModelOmniacareData.PE_AttivitaXMedico" ManuallyRouted="false" />
      </Diagram>
    </edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>

1 个答案:

答案 0 :(得分:0)

您需要添加第三个表来描述您的N到N映射,以便它基本上成为N到1到N的映射。

现在您已经创建了默认的映射策略,即1到N的两个映射。