2015-07-22 100 views
1

我有兩個表有多對多的關係。我正在使用實體框架。如何實現與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 
    } 

此代碼的作品,但我收到同樣重複的數據,所以我想以插入由子句組,和我改變了我的代碼如下:

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' 確實ñ OT,包含3爲0'CodicePersoneFisiche定義」和沒有擴展方法‘CodicePersoneFisiche’接受型的第一參數‘System.Linq.Igrouping’可以找到(是否缺少使用集引用?)

EDIT

這是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> 

回答

0

您需要添加第三個表來描述你的N到N的映射,使之成爲一個N到1到N映射基本。

現在您已經創建了默認映射策略,即兩個映射爲1到N.

+0

如何創建此第三個表?因爲在我的數據庫中有第三個表 – bircastri

+0

你可以添加你的映射代碼到你的問題嗎? – UrbanEsc

+0

是的,我只是插入映射代碼 – bircastri

相關問題