Here it is a bit simplified model of my entities
public class Location
{
[Key]
[StringLength(8)]
public string Code { get; set; }
[Required]
[StringLength(100)]
public string FriendlyName { get; set; }
public virtual ICollection<Move> Moves { get; set; }
}
public class Move
{
[Key]
public Guid Id { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
[StringLength(8)]
public string LocationFromCode { get; set; }
[Required]
[StringLength(8)]
public string LocationToCode { get; set; }
[ForeignKey("LocationFromCode")]
public Location LocationFrom { get; set; }
[ForeignKey("LocationToCode")]
public Location LocationTo { get; set; }
}
to avoid a cyclical reference issue, I have implemeted the following logic in my context
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Location>()
.HasMany(l => l.Moves)
.WithRequired(m => t.LocationFrom)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Location>()
.HasMany(a => a.Moves)
.WithRequired(t => t.LocationTo)
.WillCascadeOnDelete(false);
}
the problem I've faced to is that I can only see by a Location.Moves
only those moves that are using LocationTo->Location constraint
Is that EF restriction or am I doing something wrong?