How to automatically enable all basic navigation properties using entity infrastructure

Scenario: I would like to add an object to the database that will have navigation properties, and this object has navigation properties .. and so on. Basically, the tables in the database are related to each other - all of them.

I use EF4.3 and the context / request template, so I do not want to include Lazy load; it just takes too much time to load the object that I need. So far, I have learned that there is no other way to do this than to use an include method like this:

context.Set<TEntity>().include("navproperty1").include("navproperty1.navproperty1.1")... and so on. 

Thus, maintainability would be bad, plus a lot of code, but is there any other way if I don't want to manually record all inclusions for each type of entity?

+4
source share
2 answers

There are a lot of questions. I will try to address every point.

Firstly, lazy loading is not always faster. Especially if you download ALL relationships.

Secondly, always avoid the "magic lines". I don't know if the Include method is available, which gets a lambda expression (this is an IQueryable extension) for EF 4.3. If this is not the case, you should implement it yourself as shown here and use:

 context.Set<TEntity>().include(t => t.NavProp) 

"A" entity "has a 1: n relationship to object" B ", but" B "has an n: m relationship to entity C. And if I hadn't fallen in love with" C "in" A ", then try calling context. SaveChanges (), then all data will be lost between "B" and "C"

I do not know what you had in mind. But if you want to select a sub-navigation property that belongs to an item in the list, you should use it in EF 5: (not sure if it works in 4.3)

 context.Set<TEntity>().Include(t => t.Collection.Select(c => c.SubProp)) 

Other expressions can be found here.

If you clarify this quote, I can help more.

+6
source

take a look at this fragment

 var dbQuery = context.Letters.Where(letter => letter.ID == myId) .Include(l => l.Recipients.Select(y => y.PersonTitle))//this will include letter.Recipients.PersonTitle .Include(l => l.PersonTitle) .Include(l => l.Rank) .Include(l => l.JobTitle); theLetter = dbQuery.FirstOrDefault();// maybe null returned 

this line

 context.Letters.Include(l => l.Recipients.Select(y => y.PersonTitle)) 

will get the letter with its Recipients , and you can access the PersonTitle that exists inside the recipients navigation property

thats means withen navigation property .

0
source

Source: https://habr.com/ru/post/1479729/


All Articles