How should I override the SaveChanges () method in DbContext to support deleted / modified records using EF 4.3?

I want to save all changed records and deleted records in my project and track their status using the status column. Below is the code from my first attempt, but, for obvious reasons, I cannot distinguish OriginalValues.ToObject()from DbEntityEntry. How shoudl am I going about this?

    public override int SaveChanges()
    {
        var now = DateTime.Now;
        var currentUser = HttpContext.Current.User.Identity.Name;
        var trackedEntities = ChangeTracker.Entries<ITrackable>();

        foreach (var trackedEntity in trackedEntities)
        {
            switch(trackedEntity.State)
            {
                case System.Data.EntityState.Added:
                    trackedEntity.Entity.CreatedDate = now;                        
                    trackedEntity.Entity.CreatedBy = currentUser;                        
                    trackedEntity.Entity.Status = Status.Active;
                    break;
                case System.Data.EntityState.Modified:                        
                    var originalEntity = trackedEntity.OriginalValues.ToObject() as DbEntityEntry<ITrackable>;                        
                    originalEntity.Entity.ModifiedDate = now;
                    originalEntity.Entity.ModifiedBy = currentUser;
                    originalEntity.Entity.Status = Status.Modified;

                    var newEntity = trackedEntity.CurrentValues.ToObject() as DbEntityEntry<ITrackable>;
                    newEntity.State = System.Data.EntityState.Added;
                    newEntity.Entity.Status = Status.Active;                                                
                    break;
                case System.Data.EntityState.Deleted:
                    trackedEntity.State = System.Data.EntityState.Modified;
                    trackedEntity.Entity.Status = Status.Deleted;
                    break;                            
            }                
        }
        return base.SaveChanges();
    }
+3
source share
3 answers

, , Modified. , ToObject() OriginalValues. , originalEntity - , .

, originalEntity , , ?

:

case System.Data.EntityState.Modified:                        
    var originalEntity = trackedEntity.OriginalValues.ToObject() as ITrackable;
    originalEntity.ModifiedDate = now;
    originalEntity.ModifiedBy = currentUser;
    originalEntity.Status = Status.Modified;
    Entry(originalEntity).Status = System.Data.EntityState.Added;

    trackedEntity.Entity.Status = Status.Active;
    break;

trackedEntity , Modified originalEntity , .

, ITrackable , ToObject() , OriginalValues, ITrackable, ChangeTracker.Entries .

, , .

Edit

, originalEntity , trackedEntity - DbEntityEntry<T>. , originalEntityObject (), trackedEntityEntry.

: ( - ), ChangeTracker.DetectChanges(), foreach, , , , case ( Unchanged, ). POCO OriginalValues, base.SaveChanges, . (. " 2" : fooobar.com/questions/94552/....)

+2

, SaveChanges()

:

((ITrackable)trackedEntity.Entity).CreatedDate = now;
+2

Although it is not identical to the methodology you use, I use the repository template for this. The rest of the application simply creates an instance of the repository class and calls Save(objToSave)or Delete(objToDelete)or similar. Now the execution process is disconnected from the actual data store, and I can take the appropriate actions: update ModifyDate, change Status, etc. If you are showing DataContextfor the rest of the application, it is very difficult to control the details under the hood.

0
source

All Articles