/// <summary> /// Merge the actual database values with the object from Core Change Tracker. /// </summary> /// <param name="ctx">Database context instance.</param> /// <param name="modalEntity">The instance returned from the action.</param> /// <param name="formKeys">List of user informed fields.</param> /// <returns>Returns the merged entity.</returns> public static object Merge(this CoreDbContext ctx, IKeyed modalEntity, IEnumerable <string> formKeys) { var type = modalEntity.GetType(); var dbEntity = ctx.Entry(ctx.Find(type, modalEntity.ID)); var changedProperties = formKeys.Where(x => type.GetProperty(x) != null).ToDictionary(k => k, v => type.GetProperty(v).GetValue(modalEntity)); if (changedProperties != null && changedProperties.Count() > 0) { dbEntity.CurrentValues.SetValues(changedProperties.ToDictionary(k => k.Key, v => v.Value)); } if (changedProperties.Count(x => dbEntity.Properties.Count(y => y.Metadata.Name == x.Key) == 0) > 0) { //Set value for unmapped properties in DB var unmappedProperties = changedProperties.Where(x => dbEntity.Properties.Count(y => y.Metadata.Name == x.Key) == 0); foreach (var property in unmappedProperties) { var prop = dbEntity.Entity.GetType().GetProperty(property.Key); if (prop.SetMethod != null) //Checks if it has set method { prop.SetValue(dbEntity.Entity, property.Value); } } } return(dbEntity.Entity); }
/// <summary> /// Deletes an object from database. If the object is of type DBObject, then will only set the IsDeleted property to true, otherwise will delete. /// </summary> /// <typeparam name="T">Type of the entity.</typeparam> /// <param name="ctx">Database context instance.</param> /// <param name="id">ID of the object to be deleted.</param> /// <param name="_commitChanges">If should or not use transaction when deleting.</param> /// <returns>Returns an instance of the deleted object.</returns> public static T Delete <T>(this CoreDbContext ctx, int id, bool _commitChanges = true) where T : class, IEntity, IKeyed { var entity = ctx.Find <T>(id); if (entity != null) { if (entity is ITrackableEntity entityTrackable) { if (entity.ID > 0) { if (entity is DBObject entityDBObject) { entityDBObject.IsDeleted = true; if (_commitChanges) { ctx.SaveChanges(entityDBObject); } else { ctx.ApplyChanges(entityTrackable); } } else { ctx.Remove(entityTrackable); ctx.SaveChanges(); } } else { ctx.RemoveTrackedEntity(entity); } } else { ctx.Remove(entity); ctx.SaveChanges(); } return(entity); } throw new KeyNotFoundException(); }
public Task <TRole> FindByIdAsync(string roleId, CancellationToken cancellationToken) { var id = Convert.ToInt32(roleId); return(Task.FromResult(_ctx.Find <TRole>(id))); }