EF: Disconnection vs Impatient Load vs noTracking

I am a little confused about the meaning of the following events:

What is the difference between them?

1) AsNoTracking

- this means no contamination check. (same as changing EntityState to Detached

?)

2) context.Detach(order)

- (same as changing EntityState to Detached

?)

3) NoTracking

also required for active download?

 creativeWorkshopEntities.Job.MergeOption = MergeOption.NoTracking;
    var q = from c in creativeWorkshopEntities.Job.Include("Files")
            where c.Id == jobId                    
            select c; 

      

All I want to do is detach the object

I have an operator using

for every request in mine BL class

.

I cannot detach an object after filling the cache from the database

as I am still calling its properties on the application thread. I am guessing this will result in a runtime exception ( objectContext is desposed

)

Do you think my logic is correct?

public static Group GetMamData(string stamp, MaMDBEntities maMDBEntities)
{
    Group group = MamDataCacheManager.GetMamData(stamp);
    if (group == null)
    {
        //was not found in the cache
        //check for aveilable test with status 'start' - 1
        group = GetGroupsFromDb(stamp, maMDBEntities);

        if (group != null)
        {
            maMDBEntities.Entry(group).State = EntityState.Detached;
            MamDataCacheManager.InsertMamData(stamp, group);
        }
    }

    //option B: attache a new context
    if (maMDBEntities.Entry(group).State == EntityState.Detached)
    {
        maMDBEntities.Groups.Attach(group);
    }
    return group;
}

      

+3


source to share


1 answer


One of the main functions of ORM (EF) is change tracking: track changed / deleted / new entities so that the corresponding SQL can be generated.

But tracking changes is not free, it requires quite a lot of space and space. So when you don't need it, use it AsNoTracking

as optimization.

1) AsNoTracking - This means no pollution check. (the same as changing the EntityState for an individual?)

The end result is the same, but it's cheaper to download it without tracking than to disable it later.



2) context.Detach (order) - (same as changing EntityState to a separate one?)

I guess, yes. But I think calling Detach()

is the right way.

3) Need NoTracking for active uploads as well?

No, I do not think so.

+1


source







All Articles