-1

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?

Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38
  • You can check this https://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table – M Bakardzhiev Apr 12 '18 at 21:30
  • @MBakardzhiev Thanks! It seems there is no way to define a cummulative navigation property. Bellow I´ve posted result of my researches – Yaugen Vlasau Apr 13 '18 at 06:39

1 Answers1

0
public class Location
  {
    [Key]
    [StringLength(8)]
    public string Code { get; set; }

    [Required]
    [StringLength(100)]
    public string FriendlyName { get; set; }

    public virtual ICollection<Move> MovesTo { get; set; }
    public virtual ICollection<Move> MovesFrom { 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")]
    [InverseProperty("MovesFrom")]
    public Location LocationFrom { get; set; }

    [ForeignKey("LocationToCode")]
    [InverseProperty("MovesTo")]
    public Location LocationTo { get; set; }

}

to avoid a cyclical reference issue:

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Location>()
                .HasMany(l => l.MovesFrom)
                .WithRequired(m => t.LocationFrom)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Location>()
                .HasMany(a => a.MovesTo)
                .WithRequired(t => t.LocationTo)
                .WillCascadeOnDelete(false);

        }
Yaugen Vlasau
  • 2,148
  • 1
  • 17
  • 38