Sto usando DotNetCore 2 e voglio raggiungere uno scenario in cui ho un singolo modello, che può contenere zero o più degli stessi modelli.
Si consideri il seguente scenario (per semplicità):
Voglio collegare questi elementi in quanto tali:
Sistema
Id Name
----------------
1 Drive System
Componente
Id Name
----------------
1 Motor
2 Bearings
3 Couplings
4 Fixtures
Con la relazione molti-a-molti tra il System
e il Component
quanto tale:
Componenti del sistema
SystemId ComponentId
-------------------------
1 1
E molti a molti collegano su Component
(per la mancanza di un nome migliore e migliore)
ComponentComponents
ParentId ChildId
---------------------
1 2
1 3
1 4
Quindi, nello scenario sopra riportato può essere tradotto come Un sistema di guida ha un componente - Motore, e il motore ha sottocomponenti aggiuntivi, 'Cuscinetti', 'Giunti' e 'Elementi fissi'
La relazione SytemComponent
funziona. Per la relazione ComponentComponent
, ho provato quanto segue con la mia migrazione code-first:
Componente
public class Component
{
public Component()
{
SystemComponents = new Collection<SystemComponent>();
ChildComponents = new Collection<Component>();
}
public int Id { get; set; }
public string Name { get; set; }
// Needs this for the Many to Many relationship
public virtual ICollection<SystemComponent> SystemComponents { get; set; }
// Any component can contain one or more existing components
public virtual ICollection<Component> ChildComponents { get; set; }
}
Modello di collegamento ComponentComponent :
public class ComponentComponent
{
public int ParentComponentID { get; set; }
public int ChildComponentID { get; set; }
public Component ParentComponent { get; set; }
public Component ChildComponent { get; set; }
}
Con la mia configurazione ComponentComponent
:
public class ComponentComponentConfiguration : IEntityTypeConfiguration<ComponentComponent> {
public void Configure(EntityTypeBuilder<ComponentComponent> builder) {
builder.ToTable("ComponentComponent");
builder.HasKey(cc => new { cc.ParentComponentID, cc.ChildComponentID });
builder.HasOne(cc => cc.ParentComponent)
.WithMany(c => c.ComponentComponents)
.HasForeignKey(cc => cc.ParentComponentID);
builder.HasOne(cc => cc.ChildComponent)
.WithMany(c => c.ComponentComponents)
.HasForeignKey(cc => cc.ChildComponentID);
}
}
Durante l'esecuzione della migrazione ottengo il seguente errore:
Impossibile creare una relazione tra "Component.ComponentComponents" e "ComponentComponent.ChildComponent", poiché esiste già una relazione tra "Component.ComponentComponents" e "ComponentComponent.ParentComponent". Le proprietà di navigazione possono partecipare solo a una singola relazione.
Come potrei ottenere una tale relazione in Entity Framework in DotNetCore 2?
MODIFICARE
Ho caricato un progetto di esempio simile alla mia pagina github:
Sembra che il problema sia nella tua classe Component. Invece di fare riferimento a una raccolta di componenti dovrebbe assomigliare a questo
public class Component
{
public Component()
{
SystemComponents = new Collection<SystemComponent>();
ChildComponents = new Collection<ComponentComponent>();
}
public int Id { get; set; }
public string Name { get; set; }
// Needs this for the Many to Many relationship
public virtual ICollection<SystemComponent> SystemComponents { get; set; }
// Any component can contain one or more existing components
public virtual ICollection<ComponentComponent> ChildComponents { get; set; }
}
È necessario fare riferimento alla tabella di mappatura.
https://docs.microsoft.com/de-de/ef/core/modeling/relationships#many-to-many