0

I have two tables in my database that are linked with 2x 1 to many relations to the same object.

Since we added the second DBLot2 to the database the list in DBLot is not filled with objects anymore.

Is there something we did wrong here?

    public class DBNesting
    {
         [Key]
         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
         public long DBNestingID { get; set; }

         public DBLot DBLot { get; set; }

         [ForeignKey("DBLot")]
         public long DBLotID { get; set; }

         public DBLot DBLot2 { get; set; }

         [ForeignKey("DBLot2")]
         public long? DBLot2ID { get; set; }
    }

    public class DBLot
    {
         [Key]
         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
         public long DBLotID { get; set; }

         public List<DBNesting> Nestingen { get; set; }
    }


    This is how we get the objects:

    DatabaseContext dc = new DatabaseContext();
    dc.DBNesting
    .include("DBLot")
    .include("DBLot2")
    .where(...)
    .ToList();

    However the other side is not working:
    dc.DBLot
    .include("Nestingen")
    .where(...)
    .ToList()

    I would expect that all the DBNesting where we used a DBLot in property 
    DBLot ore DBLot2 shoud be in Nestingen. But the collections are empty.
Gho W
  • 51
  • 1
  • 7

1 Answers1

0
 dc.DBLot
    .include("Nestingen")
    .where(...)
    .ToList()

will not include the DBLot on the Nestingen only the direct object.

So it will have DBLot and a list of Nestingen but that list will not have the DBLot of each of the Nestingen in the list.

So basically you should be able to see... that you have recursion here, object has reference to object which reference itself.

dc.DBLot.include("Nestingen")
        .include("Nestingen.DBLot")
        .include("Nestingen.DBLot2")
        .where(...)
        .ToList()

may work, again will only now bring one level deeper, but if that all you need then awesome.

you could enable lazy loading... but also come with "responsibility" wouldn't recommend

ef 6 is not very efficient with include. also there is an extension which allows you to use typed version so include(x=>x.Nestingen), just take the string names out.

is the goal to have nested object relations.. nth levels. something like Tree data structure in C#

Seabizkit
  • 2,417
  • 2
  • 15
  • 32