locked
A list item in a class structure RRS feed

  • Question

  • User115640108 posted

    Do I use a correct structure of a List inside a class?
    I am trying to create a class 'ChartCompare' with a List 'CompareProfile' inside it, and the class 'ChartCompare' is an object of my db context. I can write down data into db with such structure, while when I am trying to read the data from db (context.ChartCompare), I receive only non-list items: ID and MasterTID. 

    public class Compare
        { public int ID { get; set; }
            public int TankId { get; set; }
            public bool CompareStatus { get; set; }
        }
        public class ChartCompare
        { public int ID { get; set; }
            public int MasterTID { get; set; }
           public List<Compare> CompareProfile { get; set; } 
        }



    Friday, February 12, 2021 9:43 AM

Answers

All replies

  • User753101303 posted

    Hi,

    Seems your code is matching your description.. My guess is that you are new to EF Core which doesn't use lazy loading by default (unlike EF6).

    If confirmed see https://docs.microsoft.com/en-us/ef/core/querying/related-data/ and start maybe with "eager loading" to see if it solves your issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2021 9:55 AM
  • User115640108 posted

    Hi Patrice

    Many thanks for your advise. It helped me.

    Following your suggestion, I have modified the structure so that an upper level class and a class of a list became as related items.

     public class Compare
        { public int ID { get; set; }
            public int TankId { get; set; }
            public bool CompareStatus { get; set; }
            public int? ChartCompareID { get; set; }
            public ChartCompare ChartCompare { get; set; }
        }
        public class ChartCompare
        { public int ID { get; set; }
            public int MasterTID { get; set; }
           public List<Compare> CompareProfile { get; set; } 
        }

    From other side, in this particular case MasterID is a single identification mark for a List, and such construction creates additional table just for a single variable MasterID( as in attached picture below). And it might be easier just to have a list with additional identification variable for MasterID, rather then building two realted data classes for that single identification mark.

    MSSQL structure

    Monday, February 15, 2021 11:52 AM