Is There Any Way to Determine Which Entities are Being Edited in Entity Framework 6.3

Context: converting a project that used EF 6.0 (.Net 4.7.2) to .Net Core 3.

I have upgraded the EF project to version 6.3 and am no longer using a WCF service. Instead the .edmx is being used directly. In the older project, I was able to track changes, and perform some other useful operations, using dbContext.Entities.

Is there a similar method for getting a collection of modified, added, or deleted entities using this new setup?

I should note that I am not using EF Core because of difficulties keeping the .edmx up to date; we're using database first.

1 answer

  • answered 2019-12-09 20:31 Mark Bonafe

    All credit to jcruz. In a WCF/OData service setup, I made use of dbContext.Entities to apply global settings to modified data using this technique:

    foreach (EntityDescriptor entity in context.Entities)
        UpdateEntity(entity, userId);

    Switching to .Net Core, the ability to WCF/Odata services is no longer available (I don't think). Working with the EDMX directly, the code changes only slightly. Now we use dbContext.ChangeTracker.Entries():

    using System.Data.Entity.Infrastructure;
    foreach (DbEntityEntry entry in context.ChangeTracker.Entries())
        UpdateEntry(entry, userId);

    Instead of an EntityDescriptor, we pass in a DbEntityEntry object. Other than that, UpdateEntity() is an exact copy of UpdateEntry().