Load AsNoTracking () objects with navigation properties, without specifying includes

I would like to know if the following scenario is possible with Entity Framework:

I want to load several tables using the AsNoTracking option, because they are all like static tables that the user cannot modify.

These tables are also navigational properties of others. So far, I have relied on the AutoMapping function in the Entity Framework and have not used the .Include () or LazyLoading functions.

So, instead of:

var result = from x in context.TestTable .Include("ChildTestTable") select x; 

I use it as follows:

 context.ChildTestTable.Load(); context.TestTable.Load(); var result = context.TestTable.Local; 

This works smoothly because the application is designed in such a way that the tables in the database are very small, there will not be a table that exceeds 600 rows (and this is already quite an expensive value in my application).

Now my way of loading data does not work with .AsNoTracking (). Is there any way to make it work?

Therefore, I can write:

 context.ChildTestTable.AsNoTracking().List(); var result = context.TestTable.AsNoTracking().List(); 

Instead:

 var result = from x in context.TestTable.AsNoTracking() .Include("ChildTestTable") select x; 

So, basically, I want to have one or more tables loaded by the AutoMapping function, but not loading them into the Object State Manager, is this an opportunity?

+7
c # entity-framework
source share
1 answer

The simple answer is no. For regular tracking requests, the state manager is used both to resolve the identity (search for a previously loaded instance of this object, and use it instead of creating a new instance) and fix it (connecting navigation properties together). When you use a query without tracking, this means that entities are not being tracked in the state manager. This means that patching between objects from different queries cannot occur because EF cannot find these objects.

If you want to use Include with the request for lack of tracking, EF will try to do some commit between the objects inside the request, and this will work a lot of time. However, some queries may lead to the fact that several times refer to the same object, and in some cases EF does not know that this is the same object that is referenced, and therefore, you can get duplicates.

I assume that you really do not say why you want to use no-tracking. If your tables do not have a large amount of data, then you are unlikely to see significant improvements in performance, although many factors can affect this. (As a derogation, using the ObservableCollection returned by .Local can also affect performance and should not be necessary if the data never changes.) Generally speaking, you should only use tracking if you have a clear need to do this, because otherwise case it ends up adding complexity without benefit.

+7
source share

All Articles