There is such code:

public static List<Model.Stores> SelectStoresForTable(string reg="", string rtl="", string reprInitials="") { IQueryable<Model.Stores> query = db.stores; if (!string.IsNullOrEmpty(reg)) query = query.Where(s => s.region.Name == reg); if (!string.IsNullOrEmpty(rtl)) query = query.Where(s => s.rtl.Name == rtl); if (!string.IsNullOrEmpty(reprInitials)) query = query.Where(s => s.repr.initials == rtl); return query.ToList(); } 

This code, if there are any parameters other than the default, filters out the data; if not, it returns a list of all stores. Each store has relationships with other objects (region, person, etc.). And only “simple variables” (string and id) are unloaded into the final list, and all dependencies are null (person, region, etc.). I just can not understand what the problem is. The database has these dependencies. Looks like some kind of mistake in the Entity Framework itself. I have a CodeFirst database.

If I turn on the program from the completed database, then there is an error. If I drop the tables from the running program and load the information into them again, then everything works like a clock. Maybe someone came across?

Thank you very much in advance.
Update: I received an answer to the question thanks to @Bald Another very important point, I was not connected:

 using System.Data.Entity; 

Therefore, I could not enter anonymous functions in .Include ().

  • 3
    I advise you to pay attention to this question / answer . by default, EF does not load the associated data. You must tell it about it, using Include(x=>x.Навигационное свойство) / Load() or use the lazy load. more details in the link below - Bald
  • Hi Bald. Thank you so much. I roughly understood the problem. Just the system does not load everything that can be "superfluous." And please show me an example where I need to load reg, rtl, repr (see my code) with the help of a “greedy boot” - Vladimir Borisenko
  • if in a nutshell, yes. if data is needed, it is better to use greedy Include() download - Bald
  • And please show me an example where I need to load reg, rtl, repr (see my code) using the "greedy download". That is, I have three "complex" parameters. Thank! And I need the entire list of db.stores - Vladimir Borisenko
  • one
    @VladimirBorisenko by the way, everything works on freshly beveled data most likely because you are using one long-lived context. This can be a bad decision - especially if there is a lot of data in your program, and if it does not work with the database alone. Create a new instance of the context for each sample, wrapping it in using. - PashaPash

1 answer 1

The answer to the question received thanks to @Bald:

I advise you to pay attention to this question / answer . by default, EF does not load the associated data. You must tell it about it, using Include (x => x. Navigation property) / Load () or use the lazy load. more detail in the link provided

Another very important point, I was not connected:

 using System.Data.Entity; 

Therefore, I could not enter anonymous functions in .Include (). Also @PashaPash helped me to understand why the newly loaded data worked:

@VladimirBorisenko by the way, everything works on freshly beveled data most likely because you are using one long-lived context. This can be a bad decision - especially if there is a lot of data in your program, and if it does not work with the database alone. Create a new instance of the context for each sample, wrapping it in using

Thank you all very much, guys! :-)