/// <summary> /// Adds a new business entity to the repository. /// </summary> /// <typeparam name="TEntity">The type of the entity to add.</typeparam> /// <typeparam name="TBusinessEntity">The Type of the business entity to add.</typeparam> /// <param name="businessEntity">The business entity to add to the repository.</param> public void Add <TEntity, TBusinessEntity>(TBusinessEntity businessEntity) where TEntity : DbEntity, new() where TBusinessEntity : BusinessEntity { var newEntity = new TEntity(); newEntity.InjectFrom(businessEntity); newEntity.WriteJsonData(businessEntity); Cryptography.EncryptProperties <TBusinessEntity>(newEntity); var baseDbEntity = newEntity as BaseDbEntity; if (baseDbEntity != null) { baseDbEntity.Guid = Guid.NewGuid(); baseDbEntity.CreationDate = DateTime.UtcNow.ToUniversalTime(); baseDbEntity.CreatorUsername = UserName; baseDbEntity.UpdateDate = baseDbEntity.CreationDate; baseDbEntity.UpdaterUsername = UserName; baseDbEntity.IsDeleted = false; } EventProxy.NotifyAdd(businessEntity); ImmediateDbContext.Set <TEntity>().Add(newEntity); ImmediateDbContext.SaveChanges(); ImmediateDbContext.Entry(newEntity).State = EntityState.Detached; DbContext.Set <TEntity>().Attach(newEntity); //Get updated keys businessEntity.InjectFrom(newEntity); businessEntity.Init(); EventProxy.NotifyAdded(businessEntity); }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> /// <filterpriority>2</filterpriority> public void Dispose() { if (TransactionScope != null) { TransactionScope.Dispose(); } if (DbContext != null) { DbContext.Dispose(); } if (ImmediateDbContext != null) { ImmediateDbContext.Dispose(); } }