2013-07-02 7 views
30

Ho problemi a configurare una relazione di chiave esterna nel mio Entity Framework Api fluente:problemi creando un rapporto di chiave esterna su Entity Framework

Ecco la testa del rapporto:

public class Testata 
{ 
    public Testata() { Details = new List<Dettaglio>(); } 
    public virtual int IDTEST { get; set; } 
    public virtual string Value { get; set; } 
    public virtual int IDDETAIL { get; set; } 
    public virtual string IDTESTALT { get; set; } 
    public virtual byte[] BLOB { get; set; } 

    public virtual IList<Dettaglio> Details { get; set; } 
} 

Questo è il dettaglio del rapporto

public class Dettaglio 
{ 
    public virtual int IDDETAIL { get; set; } 
    public virtual int IDTEST { get; set; } 
    public virtual string DSDETAIL { get; set; } 

    public virtual Testata TEST_TABLE { get; set; } 
} 

E questa è la mia definizione API fluida di entrambi. Responsabile del rapporto:

public TEST_TABLEMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.IDTEST) 
      .Property(t => t.IDTEST) 
      .IsRequired() 
      .HasColumnType("Int") 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) 
      .HasColumnName("IDTEST"); 


     // Table & Column Mappings 
     this.ToTable("TEST_TABLE"); 
     this.Property(t => t.Value).HasColumnName("DSVALUETEST"); 
     this.Property(t => t.IDTESTALT).HasColumnName("IDTESTALT"); 
     this.Property(t => t.BLOB).HasColumnName("BLOB"); 
    } 

dettaglio del report:

public TEST_DETAILMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.DSDETAIL); 

     // Properties 
     this.Property(t => t.DSDETAIL); 

     // Table & Column Mappings 
     this.ToTable("TEST_DETAIL"); 
     this.Property(t => t.IDDETAIL).HasColumnName("IDDETAIL"); 
     // this.Property(t => t.IDTEST).HasColumnName("IDTEST"); 
     this.Property(t => t.DSDETAIL).HasColumnName("DSDETAIL"); 

     // Relationships 
     this.HasOptional(t => t.TEST_TABLE) 
      .WithMany(t => t.Details) 
      .HasForeignKey(d => d.IDDETAIL).WillCascadeOnDelete(true); 

    } 

In fase di esecuzione ottengo sempre questo errore

System.Data.Entity.Edm.EdmAssociationType:: Molteplicità conflitti con il vincolo referenziale nel ruolo "Dettaglio_TEST_TABLE_Target" nella relazione "Dettaglio_TEST_TABLE". Poiché tutte le proprietà nel ruolo dipendente non sono annullabili, la molteplicità del ruolo principale deve essere "1".

Quale, credo, significa che sto fallendo qualcosa a definizione di chiave esterna, ma non so davvero dove guardare. Qualsiasi aiuto/suggerimento è molto apprezzato.

risposta

89

C'è un conflitto tra la vostra proprietà chiave esterna in classe Dettaglio ...

public virtual int IDTEST { get; set; } 

... che ha un tipo non nullable (int) e quindi non può essere facoltativa e la mappatura ...

this.HasOptional(t => t.TEST_TABLE) //... 

... dove si desidera che la relazione sia facoltativa.

Se si desidera davvero un rapporto opzionale utilizzare una annullabile proprietà FK:

public virtual int? IDTEST { get; set; } 

altrimenti si deve usare HasRequired per un rapporto richiesto con una proprietà FK non annullabile.

+1

Grazie mille :) che ha risolto – user2541621

+1

Questa è una buona risposta – abelabbesnabi

+2

Il '?' è stata l'aggiunta chiave nel mio caso. – jonas