locked
How to disable tracking on DomainContext ? RRS feed

  • Question

  • Hi,

    I'm saving tree structured data in a self referenced table.

    To do this I recursively add the child items to my EntityCollection.

    But for large data this takes forever (several minutes for 10000 items).

    I suppose this is because of the data tracking, but I don't know how to disable it.

    Is it possible ?

    Thanks for your help

    Tuesday, December 16, 2014 2:30 PM

Answers

  • Finally I've used the "simple" solution: Transmit the data through arrays to the server and rebuild the data from there. Is still slow (almost one minute to insert 6500 rows in the database)but it's far better than before.

    Thanks for your help.

    • Marked as answer by Jonny_D Monday, December 29, 2014 3:32 AM
    Tuesday, December 23, 2014 1:27 PM

All replies

  • I think that's

    context.Configuration.AutoDetectChangesEnabled = false;

    And I think the advice here is applicable:

    http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Tuesday, December 16, 2014 4:01 PM
    Moderator
  • Thank you for your answer. But I work with a DomainContext, not a DbContext.

    Or perhaps I don't understand your answer, I'm quite new to Entity Framework.

    Tuesday, December 16, 2014 4:42 PM
  • Ah yes, everything changes with EF6.

    .

    Where does the data come from if you're adding it to the context?

    Presumably not ria if you're constructing it.

    Can't you construct joins etc first?

    Why do you build it in the context?

    You know entities are just objects - you can just build a collection of them?

    Can you build the structure and then attach?

    You would of course not know which had any changes if there is no context attached though.

    10,000 records seems a lot to be manipulating at once.


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Tuesday, December 16, 2014 5:35 PM
    Moderator
  • The application is a kind of vector drawing tool.

    The problematic data are just used to load and save the drawings, and are not used anywhere else, I don't need the property change notification, but I don't know how to save the data in the database without using the DomainContext.

    Thanks again for your help.

    Tuesday, December 16, 2014 11:09 PM
  • Hi,

    You could try to set AsNoTracking per each query or per each ObjectSet (not DbSet). The latter approach requires using ObjectContext API.

    var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
    
    var set = objectContext.CreateObjectSet<T>();
    set.MergeOption = MergeOption.NoTracking;
    // And use set for queries
    

    Besides, check these threads:

    http://stackoverflow.com/questions/12726878/global-setting-for-asnotracking

    http://stackoverflow.com/questions/18925111/turn-off-ef-change-tracking-for-any-instance-of-the-context

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, December 17, 2014 3:30 AM
  • Thanks for your answer, but as I said to Andy ONeill I work with a DomainContext, not a DbContext.

    Best Regards

    Wednesday, December 17, 2014 8:22 AM
  • The application is a kind of vector drawing tool.

    The problematic data are just used to load and save the drawings, and are not used anywhere else, I don't need the property change notification, but I don't know how to save the data in the database without using the DomainContext.

    Thanks again for your help.

    You haven't answered hardly any of my questions.

    You can obtain objects, create them or whatever and then attach them to the context, mark as changed in order to update data.

    If you read this data via ria you can then detach it from the domaincontext.

    http://akashkava.com/blog/379/detach-entities-recursively-and-attach-to-new-domaincontext-in-entity-framework-and-ria-services/

    You would work with EntitySet

    EntitySet esSite = context.EntityContainer.GetEntitySet(typeof(Site));
    

    You can override some aspects of ria and I suspect you could probably change tracking behaviour.

    For example, if you look at EntitySet:

    http://msdn.microsoft.com/en-us/library/system.servicemodel.domainservices.client.entityset(v=vs.91).aspx

    This is:

    public abstract class EntitySet : IEnumerable, INotifyCollectionChanged,

    IRevertibleChangeTracking,

    IChangeTracking, INotifyPropertyChanged


    You could probably inherit and supply your own IChangeTracking that you can switch on and off.

    .

    Alternatively.

    If this all seems a bit hard then you could read this data using a regular wcf service without any context stuff and obtain your collection that way.

    Then when you want to update, attach each to your context and mark as changed.


    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

    Wednesday, December 17, 2014 10:38 AM
    Moderator
  • Finally I've used the "simple" solution: Transmit the data through arrays to the server and rebuild the data from there. Is still slow (almost one minute to insert 6500 rows in the database)but it's far better than before.

    Thanks for your help.

    • Marked as answer by Jonny_D Monday, December 29, 2014 3:32 AM
    Tuesday, December 23, 2014 1:27 PM