Entity Framework Many To Many returns null

224 Views Asked by At

I have Many To Many relationship defined and when I try to query for the records that should be in the map I get null.

    public class Record
    {
        public int RecordId { get; set; }
        public DateTime DateRecordCreated { get; set; }

        public ICollection<Street> Streets { get; set; }
        public ICollection<Street> CrossStreets { get; set; } 
    }


    public class RecordMap : EntityTypeConfiguration<Record>
    {
        public RecordMap()
        {
            // Primary Key
            this.HasKey(t => t.RecordId);



            this.HasMany(r => r.Streets)
                .WithMany(c => c.Records)
                .Map(sl =>
                {
                    sl.ToTable("StreetRecordMap", "dbo");
                    sl.MapLeftKey("RecordId");
                    sl.MapRightKey("StreetId");
                });

            this.HasMany(r => r.CrossStreets)
                .WithMany(c => c.AnotherRecord)
                .Map(sl =>
                {
                    sl.ToTable("AnotherStreetRecordMap", "dbo");
                    sl.MapLeftKey("RecordId");
                    sl.MapRightKey("StreetId");
                });

            this.Property(t => t.DateRecordCreated).IsRequired();
        }
}


    public class House : Record
    {
        public string HouseNumber { get; set; }
        public string StreeName { get; set; }
        public int ZipCode { get; set; }

    }



    public class Street
    {
        public int StreetId { get; set; }
        public string StreetName { get; set; }

        public ICollection<Record> Records { get; set; }
        public ICollection<Record> AnotherRecord { get; set; } 
    }

Now when I run the following query below I get houses.CrossStreets as null, I tried adding enabling lazy loading and had the same out come.

 public static void GetRecords()
    {
        using (var context = new SandboxContext())
        {

            var entities = context.Houses.Include(r => r.CrossStreets);

            var houses = entities.ToList();
        }
    }
0

There are 0 best solutions below