public async Task Save <TModel, TModelKeyType, TEntity, TEntityKeyType>(TModel model, CancellationToken cancellationToken, EntityState entityState, bool updateModelAfterCommit = false) where TModel : class, IKey <TModelKeyType> where TModelKeyType : IEquatable <TModelKeyType> where TEntity : class, IKey <TEntityKeyType> where TEntityKeyType : IEquatable <TEntityKeyType> { using (IUnitOfWorkScope <TUnitOfWork> scope = UnitOfWorkScopeFactory.Create(UowFactory)) { DbUnitOfWork dbContext = scope.UnitOfWork as DbUnitOfWork; if (dbContext == null) { throw new InvalidOperationException("Unsupported Unit of Work."); } dbContext.Entry(model).State = entityState; await scope.CommitAsync(cancellationToken); } }
public async Task Save <TModel, TModelKeyType, TEntity, TEntityKeyType>(TModel model, CancellationToken cancellationToken, EntityState entityState, bool updateModelAfterCommit = false) where TModel : class, IKey <TModelKeyType> where TModelKeyType : IEquatable <TModelKeyType> where TEntity : class, IKey <TEntityKeyType> where TEntityKeyType : IEquatable <TEntityKeyType> { using (IUnitOfWorkScope <TUnitOfWork> scope = UnitOfWorkScopeFactory.Create(UowFactory)) { DbUnitOfWork dbContext = scope.UnitOfWork as DbUnitOfWork; if (dbContext == null) { throw new InvalidOperationException("Unsupported Unit of Work."); } var entityEntry = dbContext.ChangeTracker.Entries <TEntity>() .Where(e => !e.Entity.Id.Equals(default(TEntityKeyType))) .Where(e => typeof(TEntityKeyType) == typeof(TModelKeyType) ? e.Entity.Id.Equals(model.Id) : e.Entity.Id.ToString() == model.Id.ToString()) .SingleOrDefault(); if (entityEntry == null) { TEntity entity = Mapper.Map <TModel, TEntity>(model); entityEntry = dbContext.Entry(entity); } entityEntry.State = entityState; if (updateModelAfterCommit) { switch (entityState) { case EntityState.Added: case EntityState.Modified: dbContext.OnCommit += (db, e) => Mapper.Map(entityEntry.Entity, model); // Refresh model in case of DB-assigned values (Identity/timestamp/etc.). break; } } await scope.CommitAsync(cancellationToken); } }