I want to build a relation like this ( a Zone is in the neighbourhood of x other zones )
public class Zone
{
public string Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ZoneNeighourhood> ZoneNeighourhoods { get; set; }
}
public class ZoneNeighbourhood
{
public virtual Zone Zone1 { get; set; }
public virtual Zone Zone2 { get; set; }
}
Unfortunately this won’t work, because the FKs generated by EF are not correct… How can i get a structure like this to work?
Example with 3 Zones: Zone 1, Zone 2, Zone 3
Zone 1 Neighours:
Zone 2,
Zone 3
Zone 2 Neighbours:
Zone 1
Zone 3 Neighbours:
Zone1
Any advice?
Your mapping is not correct. You are creating self referencing entity so you need separate collection for incoming and outgoing relations. Single collection is not enough.
You don’t need to map junction table unless you also want to add some additional properties to the relation.
If you want only single collection you must use fluent mapping: