5

Sto usando Database Entity Framework In primo luogo, ma vorrei replicare il seguente comportamento del Codice primo paradigma:Entity Framework DB Primo: Converti tabella associativa di proprietà di navigazione

In Entity Framework Codice In primo luogo, si può fare qualcosa del genere:

public class Thing 
{ 
    public int ID { get; set; } 
    ICollection<Stuff> Stuffs { get; set; } 
} 

public class Stuff 
{ 
    public int ID { get; set; } 
    ICollection<Thing> Things { get; set; } 
} 

E il database genererà e la tabella associativa rappresenterà la relazione molti a molti.

Sto utilizzando Database First con un database precedente. Ho inserito le entità e incluso la tabella associativa che rappresenta una relazione molti-a-molti tra due delle nostre tabelle.

Poiché la tabella associativa è incluso come entità, le proprietà di navigazione sono in quanto tali:

public class Thing 
{ 
    public int ID { get; set; } 
    public ICollection<ThingStuff> ThingStuffs { get; set; } 
} 

public class ThingStuff 
{ 
    public int ThingID { get; set; } 
    public int StuffID { get; set; } 
    ICollection<Thing> Things { get; set; } 
    ICollection<Stuff> Stuffs { get; set; } 
} 

public class Stuff 
{ 
    public int ID { get; set; } 
    public ICollection<ThingStuff> ThingStuffs { get; set; } 
} 

Quindi, per navigare, devo:

var stuff = Thing.ThingStuffs.Select(ts => ts.Stuff); 

Invece di:

var stuff = Thing.Stuffs; 

Quindi la domanda è:

C'è un modo per rilasciare l'entità che rappresenta l'associazione (ThingStuff) e comunicare a EntityFramework la tabella esistente per creare le proprietà di navigazione many-to-many?

+1

Penso che la tabella di giunzione reale avesse più attributi che solo le due chiavi esterne o una chiave primaria separata, altrimenti EF non l'avrebbe incorporata nel modello concettuale. È giusto? –

+0

Oltre a ciò che @GertArnold ha detto se una delle chiavi esterne è nullable, il database EF in primo luogo include anche la tabella associata come entità separata. –

risposta

Problemi correlati