Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I encountered the same issue and finally came up with a solution. The basic idea is to prevent certain navigational class type from attaching to the ObjectContext. Here is what I did:</p> <ol> <li>Modified the context.tt template to make SelfTrackingEntitiesContextExtensions class partial.</li> <li>Copy the 2 ApplyChanges functions to newly created Custom.Context.Extension.cs and rename them as CustomApplyChanges. Each will have one additional parameter as array of Type</li> </ol> <blockquote> <p>public static void CustomApplyChanges(this ObjectContext context, string entitySetName, TEntity entity, <strong>Type[] excludeTypes</strong>) where TEntity : IObjectWithChangeTracker</p> </blockquote> <ol start="3"> <li>Add a condition to the for loop to exclude any class type contains in the excludeTypes array.</li> </ol> <blockquote> <p>region Handle Initial Entity State</p> <p>foreach (IObjectWithChangeTracker changedEntity in entityIndex.AllEntities.Where(x => x.ChangeTracker.State == ObjectState.Deleted &amp;&amp; <strong>!excludeTypes.Contains(x.GetType())))</strong> { HandleDeletedEntity(context, entityIndex, allRelationships, changedEntity); }</p> <p>foreach (IObjectWithChangeTracker changedEntity in entityIndex.AllEntities.Where(x => x.ChangeTracker.State != ObjectState.Deleted <strong>&amp;&amp; !excludeTypes.Contains(x.GetType())))</strong> { HandleEntity(context, entityIndex, allRelationships, changedEntity); }</p> <p>endregion</p> </blockquote> <ol start="4"> <li>The usage</li> </ol> <blockquote> <p>Type[] excludeTypes = { typeof(Asset), typeof(Address), typeof(Region)};</p> <p>rep.Entities.CustomApplyChanges(entity, excludeTypes); var changedEntry = rep.Context.ObjectStateManager.GetObjectStateEntries>(System.Data.EntityState.Added | System.Data.EntityState.Modified | System.Data.EntityState.Deleted); foreach (var e in changedEntry) { if (excludeTypes.Any(c => c == e.Entity.GetType())) { rep.Context.Detach(e.Entity); //detach unchanged objects } }</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload