public virtual void Update <T>(T entity, params Expression <Func <T, object> >[] updatedProperties) where T : class { //dbEntityEntry.State = EntityState.Modified; --- I cannot do this. //Ensure only modified fields are updated. var dbEntityEntry = _context.Entry(entity); if (updatedProperties.Any()) { //update explicitly mentioned properties foreach (var property in updatedProperties) { dbEntityEntry.Property(property).IsModified = true; } } else { //no items mentioned, so find out the updated entries foreach (var property in dbEntityEntry.OriginalValues.PropertyNames) { var original = dbEntityEntry.OriginalValues.GetValue <object>(property); var current = dbEntityEntry.CurrentValues.GetValue <object>(property); if (original != null && !original.Equals(current)) { dbEntityEntry.Property(property).IsModified = true; } } } }
private void Save() { //MyUtils.ValidateSessions(Exchange.Sessions.ToList<ISession>(), out List<string> errors); var nameExists = context.Exchanges.Any(x => x.Name == Exchange.Name); if (nameExists && originalExchange?.Name != Exchange.Name) { MessageBus.Current.SendMessage("Name already exists, please change it."); return; } if (Title == TitleForAdd) { ExchangesViewModel.Exchanges.Add(Exchange); } else { //_context.Exchanges.Attach(_originalExchange); context.Entry(originalExchange).CurrentValues.SetValues(Exchange); context.SaveChanges(); } }