private static T InnerGetCopy <T>(IDbContext context, T currentCopy, Func <DbEntityEntry <T>, DbPropertyValues> func) where T : BaseEntity { //Get the database context //获取数据库上下文 DbContext dbContext = CastOrThrow(context); //Get the entity tracking object //获取实体跟踪对象 DbEntityEntry <T> entry = GetEntityOrReturnNull(currentCopy, dbContext); //The output //输出 T output = null; //Try and get the values //尝试得到这些值 if (entry != null) { DbPropertyValues dbPropertyValues = func(entry); if (dbPropertyValues != null) { output = dbPropertyValues.ToObject() as T; } } return(output); }
private static void AddHistoricalEntity(DbContext dbContext, DbPropertyValues originalValues, DateTimeOffset moment) { var original = Mapper.Map <THistoricalEntity>(originalValues.ToObject()); original.ValidTo = moment; dbContext.Set <THistoricalEntity>().Add(original); }
private void buttonSave_Click(object sender, RoutedEventArgs e) { try { int effected = 0; DbChangeTracker tracker = entity.ChangeTracker; IEnumerable <DbEntityEntry> entries = tracker.Entries(); Console.WriteLine(entity.Employees.Count()); //dataGrid.ItemsSource = entries.ToList(); if (tracker.HasChanges()) { IEnumerable <DbEntityEntry> modifiedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Modified); List <Employee> changes = new List <Employee>(); foreach (DbEntityEntry entityEntry in modifiedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); changes.Add(employee); } IEnumerable <DbEntityEntry> deletedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Deleted || n.State == System.Data.Entity.EntityState.Detached); List <Employee> deletes = new List <Employee>(); foreach (DbEntityEntry entityEntry in deletedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); deletes.Add(employee); } IEnumerable <DbEntityEntry> addedEntries = entries.Where(n => n.State == System.Data.Entity.EntityState.Added); List <Employee> adds = new List <Employee>(); foreach (DbEntityEntry entityEntry in addedEntries) { //DbPropertyValues oldValues = entityEntry.GetDatabaseValues(); DbPropertyValues newValues = entityEntry.CurrentValues; Employee employee = (Employee)newValues.ToObject(); adds.Add(employee); } ItemChanges itemChanges = new ItemChanges(); itemChanges.Topmost = true; itemChanges.dataGridChanges.ItemsSource = changes.ToList(); itemChanges.dataGridChangesDelete.ItemsSource = deletes.ToList(); itemChanges.dataGridChangesNew.ItemsSource = adds.ToList(); itemChanges.ShowDialog(); if (itemChanges.IsAccepted) { effected = entity.SaveChanges(); MessageBox.Show("Success to update data: " + effected.ToString() + " item(s)."); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public T GetDetachedOriginal <T>(T entity, bool fixupRelationships = false) where T : class { DbPropertyValues originalValues = this.context.Entry <T>(entity).OriginalValues; T orig = (T)originalValues.ToObject(); if (fixupRelationships) { throw new NotImplementedException(); } return(orig); }
protected object GetOriginalValue(DbUpdateConcurrencyException conflict) { DbEntityEntry entry = conflict != null && conflict.Entries != null?conflict.Entries.FirstOrDefault() : null; if (entry == null) { return(null); } DbPropertyValues values = entry.GetDatabaseValues(); return(values != null?values.ToObject() : null); }
protected virtual object GetOriginalValue(DbUpdateConcurrencyException conflict) { DbEntityEntry entity = conflict != null && conflict.Entries != null?conflict.Entries.FirstOrDefault() : null; if (entity == null) { return(null); } DbPropertyValues values = entity.GetDatabaseValues(); object model = values != null?values.ToObject() : null; return(model != null?Mapper.Map <TData>(model) : null); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="currentCopy"></param> /// <param name="func"></param> /// <returns></returns> private static T InnerGetCopy <T>(IDbContext context, T currentCopy, Func <DbEntityEntry <T>, DbPropertyValues> func) where T : BaseEntity { DbContext dbContext = CastOrThrow(context); DbEntityEntry <T> entry = GetEntityOrReturnNull(currentCopy, dbContext); T output = null; if (entry != null) { DbPropertyValues dbPropertyValues = func(entry); if (dbPropertyValues != null) { output = dbPropertyValues.ToObject() as T; } } return(output); }
private static T InnerGetCopy <T>(IDbContext context, T currentCopy, Func <DbEntityEntry <T>, DbPropertyValues> func) where T : BaseEntity { // Get the database context var dbContext = CastOrThrow <DbContext>(context); // Get the entity tracking object DbEntityEntry <T> entry = GetEntityOrDefault(currentCopy, dbContext); // The output T output = null; // Try and get the values if (entry != null) { DbPropertyValues dbPropertyValues = func(entry); if (dbPropertyValues != null) { output = dbPropertyValues.ToObject() as T; } } return(output); }
private static T InnerGetCopy <T>(IEntityFrameworkRepositoryContext context, T currentCopy, Func <DbEntityEntry <T>, DbPropertyValues> func) where T : Entity { //Get the database context DbContext dbContext = CastOrThrow(context); //Get the entity tracking object DbEntityEntry <T> entry = GetEntityOrReturnNull(currentCopy, dbContext); //The output T output = null; //Try and get the values if (entry != null) { DbPropertyValues dbPropertyValues = func(entry); if (dbPropertyValues != null) { output = dbPropertyValues.ToObject() as T; } } return(output); }
public void Non_Generic_DbPropertyValues_uses_ToObject_on_InternalPropertyValues() { var properties = new Dictionary<string, object> { { "Id", 1 } }; var values = new DbPropertyValues(new TestInternalPropertyValues<FakeTypeWithProps>(properties)); var clone = (FakeTypeWithProps)values.ToObject(); Assert.Equal(1, clone.Id); }
public override async Task <int> SaveChangesAsync(CancellationToken cancellationToken) { int num; try { int changes = 0; this.SyncObjectsStatePreCommit(); if (this.ChangeTracker.HasChanges()) { List <object> logList = new List <object>(); List <IWorkflowStarterEntity> list = new List <IWorkflowStarterEntity>(); foreach (DbEntityEntry entry in this.ChangeTracker.Entries()) { DbEntityEntry dbEntry = entry; if (dbEntry.State != System.Data.Entity.EntityState.Detached && dbEntry.State != System.Data.Entity.EntityState.Unchanged) { if (ConfigurationController.CustomIdentityEnabled && dbEntry.State == System.Data.Entity.EntityState.Added && dbEntry.Entity is IEntity && dbEntry.Entity is ICustomIdentity) { IEntity entity = dbEntry.Entity as IEntity; string sequenceName = (dbEntry.Entity as ICustomIdentity).IdentityGeneratorSequenceName; int identity = this.Database.SqlQuery <int>(string.Format("select {0}.NEXTVAL from dual", (object)sequenceName)).FirstOrDefault <int>(); entity.SetID((object)identity); entity = (IEntity)null; sequenceName = (string)null; } if (dbEntry.Entity is IActivityLoggable) { IActivityLoggable entityForLog = (IActivityLoggable)dbEntry.Entity; if (entityForLog.ActionsToLog == ActionLog.All) { logList.Add(dbEntry.Entity); } else if (dbEntry.State == System.Data.Entity.EntityState.Added && (entityForLog.ActionsToLog & ActionLog.Insert) == ActionLog.Insert) { logList.Add(dbEntry.Entity); } else if (dbEntry.State == System.Data.Entity.EntityState.Modified && (entityForLog.ActionsToLog & ActionLog.Update) == ActionLog.Update) { logList.Add(dbEntry.Entity); } else if (dbEntry.State == System.Data.Entity.EntityState.Deleted && (entityForLog.ActionsToLog & ActionLog.Delete) == ActionLog.Delete) { logList.Add(dbEntry.Entity); } entityForLog = (IActivityLoggable)null; } if (dbEntry.Entity is ILoggableEntityID) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(cancellationToken); ILoggableEntityID dbEntity = dbValues.ToObject() as ILoggableEntityID; if (dbEntity != null) { ILoggableEntityID loggableEntity = (ILoggableEntityID)dbEntry.Entity; loggableEntity.LogData = new EntityIDLogData(); loggableEntity.LogData.InsertUserID = dbEntity.LogData.InsertUserID; loggableEntity.LogData.InsertDateTime = dbEntity.LogData.InsertDateTime; loggableEntity.LogData.UpdateUserID = new int?(SecurityManager.CurrentUserContext == null ? -1 : SecurityManager.CurrentUserContext.UserId); loggableEntity.LogData.UpdateDateTime = new DateTime?(DateTime.Now); loggableEntity = (ILoggableEntityID)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableEntityID)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableEntityID loggableEntity = (ILoggableEntityID)dbEntry.Entity; loggableEntity.LogData = new EntityIDLogData(); loggableEntity.LogData.InsertUserID = new int?(SecurityManager.CurrentUserContext == null ? -1 : SecurityManager.CurrentUserContext.UserId); loggableEntity.LogData.InsertDateTime = new DateTime?(DateTime.Now); loggableEntity.LogData.UpdateUserID = new int?(); loggableEntity.LogData.UpdateDateTime = new DateTime?(); loggableEntity = (ILoggableEntityID)null; } } else if (dbEntry.Entity is ILoggableEntityName) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(); ILoggableEntityName dbEntity = dbValues.ToObject() as ILoggableEntityName; if (dbEntity != null) { ILoggableEntityName loggableEntity = (ILoggableEntityName)dbEntry.Entity; loggableEntity.LogData = new EntityNameLogData(); loggableEntity.LogData.InsertUserName = dbEntity.LogData.InsertUserName; loggableEntity.LogData.InsertDateTime = dbEntity.LogData.InsertDateTime; loggableEntity.LogData.UpdateUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.UpdateDateTime = new DateTime?(DateTime.Now); loggableEntity = (ILoggableEntityName)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableEntityName)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableEntityName loggableEntity = (ILoggableEntityName)dbEntry.Entity; loggableEntity.LogData = new EntityNameLogData(); loggableEntity.LogData.InsertUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.InsertDateTime = DateTime.Now; loggableEntity.LogData.UpdateUserName = (string)null; loggableEntity.LogData.UpdateDateTime = new DateTime?(); loggableEntity = (ILoggableEntityName)null; } } else if (dbEntry.Entity is ILoggableEntityNameAndID) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(); ILoggableEntityNameAndID dbEntity = dbValues.ToObject() as ILoggableEntityNameAndID; if (dbEntity != null) { ILoggableEntityNameAndID loggableEntity = (ILoggableEntityNameAndID)dbEntry.Entity; loggableEntity.LogData = new EntityNameAndIDLogData(); loggableEntity.LogData.InsertUserId = dbEntity.LogData.InsertUserId; loggableEntity.LogData.InsertUserName = dbEntity.LogData.InsertUserName; loggableEntity.LogData.InsertDateTime = dbEntity.LogData.InsertDateTime; loggableEntity.LogData.UpdateUserId = new int?(SecurityManager.CurrentUserContext == null ? -1 : SecurityManager.CurrentUserContext.UserId); loggableEntity.LogData.UpdateUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.UpdateDateTime = new DateTime?(DateTime.Now); loggableEntity = (ILoggableEntityNameAndID)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableEntityNameAndID)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableEntityNameAndID loggableEntity = (ILoggableEntityNameAndID)dbEntry.Entity; loggableEntity.LogData = new EntityNameAndIDLogData(); loggableEntity.LogData.InsertUserId = SecurityManager.CurrentUserContext == null ? -1 : SecurityManager.CurrentUserContext.UserId; loggableEntity.LogData.InsertUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.InsertDateTime = DateTime.Now; loggableEntity.LogData.UpdateUserId = new int?(); loggableEntity.LogData.UpdateUserName = (string)null; loggableEntity.LogData.UpdateDateTime = new DateTime?(); loggableEntity = (ILoggableEntityNameAndID)null; } } else if (dbEntry.Entity is ILoggableMCIEntity) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(); ILoggableMCIEntity dbEntity = dbValues.ToObject() as ILoggableMCIEntity; if (dbEntity != null) { ILoggableMCIEntity loggableEntity = (ILoggableMCIEntity)dbEntry.Entity; loggableEntity.LogData = new MCIEntityLogData(); loggableEntity.LogData.InsertUserName = dbEntity.LogData.InsertUserName; loggableEntity.LogData.InsertDate = dbEntity.LogData.InsertDate; loggableEntity.LogData.InsertTime = dbEntity.LogData.InsertTime; loggableEntity.LogData.UpdateUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.UpdateDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.UpdateTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity = (ILoggableMCIEntity)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableMCIEntity)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableMCIEntity loggableEntity = (ILoggableMCIEntity)dbEntry.Entity; loggableEntity.LogData = new MCIEntityLogData(); loggableEntity.LogData.InsertUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.InsertDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.InsertTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity.LogData.UpdateUserName = (string)null; loggableEntity.LogData.UpdateDate = (string)null; loggableEntity.LogData.UpdateTime = (string)null; loggableEntity = (ILoggableMCIEntity)null; } } else if (dbEntry.Entity is ILoggableMCIEntity2) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(); ILoggableMCIEntity2 dbEntity = dbValues.ToObject() as ILoggableMCIEntity2; if (dbEntity != null) { ILoggableMCIEntity2 loggableEntity = (ILoggableMCIEntity2)dbEntry.Entity; loggableEntity.LogData = new MCIEntityLogData2(); loggableEntity.LogData.InsertUserName = dbEntity.LogData.InsertUserName; loggableEntity.LogData.InsertDate = dbEntity.LogData.InsertDate; loggableEntity.LogData.InsertTime = dbEntity.LogData.InsertTime; loggableEntity.LogData.UpdateUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.UpdateDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.UpdateTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity = (ILoggableMCIEntity2)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableMCIEntity2)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableMCIEntity2 loggableEntity = (ILoggableMCIEntity2)dbEntry.Entity; loggableEntity.LogData = new MCIEntityLogData2(); loggableEntity.LogData.InsertUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.InsertDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.InsertTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity.LogData.UpdateUserName = (string)null; loggableEntity.LogData.UpdateDate = (string)null; loggableEntity.LogData.UpdateTime = (string)null; loggableEntity = (ILoggableMCIEntity2)null; } } else if (dbEntry.Entity is ILoggableMCIEntityWithIP) { if (dbEntry.State == System.Data.Entity.EntityState.Modified) { DbPropertyValues dbValues = await dbEntry.GetDatabaseValuesAsync(); ILoggableMCIEntityWithIP dbEntity = dbValues.ToObject() as ILoggableMCIEntityWithIP; if (dbEntity != null) { ILoggableMCIEntityWithIP loggableEntity = (ILoggableMCIEntityWithIP)dbEntry.Entity; loggableEntity.LogData = new MCIEntityWithIPLogData(); loggableEntity.LogData.InsertUserName = dbEntity.LogData.InsertUserName; loggableEntity.LogData.InsertDate = dbEntity.LogData.InsertDate; loggableEntity.LogData.InsertTime = dbEntity.LogData.InsertTime; loggableEntity.LogData.InsertUserIP = dbEntity.LogData.InsertUserIP; loggableEntity.LogData.UpdateUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.UpdateDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.UpdateTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity.LogData.UpdateUserIP = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.ClientIP; loggableEntity = (ILoggableMCIEntityWithIP)null; } dbValues = (DbPropertyValues)null; dbEntity = (ILoggableMCIEntityWithIP)null; } else if (dbEntry.State == System.Data.Entity.EntityState.Added) { ILoggableMCIEntityWithIP loggableEntity = (ILoggableMCIEntityWithIP)dbEntry.Entity; loggableEntity.LogData = new MCIEntityWithIPLogData(); loggableEntity.LogData.InsertUserName = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.UserName; loggableEntity.LogData.InsertDate = DateTime.Now.ToPersianDateTime().ToDateString(); loggableEntity.LogData.InsertTime = DateTime.Now.ToPersianDateTime().ToTimeString(); loggableEntity.LogData.InsertUserIP = SecurityManager.CurrentUserContext == null ? "-1" : SecurityManager.CurrentUserContext.ClientIP; loggableEntity.LogData.UpdateUserName = (string)null; loggableEntity.LogData.UpdateDate = (string)null; loggableEntity.LogData.UpdateTime = (string)null; loggableEntity.LogData.UpdateUserIP = (string)null; loggableEntity = (ILoggableMCIEntityWithIP)null; } } if (dbEntry.Entity is ICacheable) { CacheManager.Remove(dbEntry.Entity.GetType().FullName); } if (dbEntry.Entity is IConcurrencySupportable && (dbEntry.State == System.Data.Entity.EntityState.Modified || dbEntry.State == System.Data.Entity.EntityState.Added)) { dbEntry.Property("RowVersion").CurrentValue = (object)Guid.NewGuid().ToString().Replace("-", ""); } dbEntry = (DbEntityEntry)null; } } changes = await base.SaveChangesAsync(cancellationToken); List <OMF.Common.ActivityLog.ActivityLog> activityLogList = new List <OMF.Common.ActivityLog.ActivityLog>(); foreach (object obj in logList) { object entity = obj; OMF.Common.ActivityLog.ActivityLog activityLog = ActivityLogManager.CreateActivityLog(entity as IActivityLoggable); activityLogList.Add(activityLog); activityLog = (OMF.Common.ActivityLog.ActivityLog)null; entity = (object)null; } this.SyncObjectsStatePostCommit(); await ActivityLogManager.SaveAsync((IEnumerable <OMF.Common.ActivityLog.ActivityLog>) activityLogList); logList = (List <object>)null; list = (List <IWorkflowStarterEntity>)null; activityLogList = (List <OMF.Common.ActivityLog.ActivityLog>)null; } num = changes; } catch (DbEntityValidationException ex) { string jointFailures = string.Join(Constants.NewLine, ex.EntityValidationErrors.SelectMany <DbEntityValidationResult, string>((Func <DbEntityValidationResult, IEnumerable <string> >)(failure => failure.ValidationErrors.Select <DbValidationError, string>((Func <DbValidationError, string>)(error => string.Format("{0}: {1}", (object)error.PropertyName, (object)error.ErrorMessage)))))); throw new DataAccessException(jointFailures, (Exception)ex); } return(num); }
public object ToObject() { return(_dbPropertyValues.ToObject()); }