简体   繁体   中英

How to implement many to many relationship with Entity Framework?

I have two tables with a many to many relationship. I'm using Entity Framework.

These are my model classes:

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; }
}

In my database, there is a third table to implement the many to many relationship.

Now I want to extract all data from this table, so I wrote this code:

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

This code works but I received also duplicate data, so I thought to insert the clause Group by, and I have changed my code to this :

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
    }

but I received this error from compiler:

'System.Linq.Igrouping' does not contai a definition for 0'CodicePersoneFisiche' and no extension method 'CodicePersoneFisiche' accepting a first argument of type 'System.Linq.Igrouping' could be found (are you missing a using assembly reference?)

EDIT

This is the 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>

You will need to add a third table to describe your N to N mapping so that it becomes a N to 1 to N mapping basically.

Right now you have created the default mapping strategy, that is two mappings of 1 to N.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM