rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

Force all columns to be written to database #215

Closed rubenalves closed 1 year ago

rubenalves commented 1 year ago

Hello,

is i do something like this EntityHelper.GetRecord(teste).Status == EntityStatus.Modified; session.SaveChanges(); does all the columns go to the database ? Thanks.

rivantsov commented 1 year ago

no, only those that are actually modified (assigned); it is internal optimization

rubenalves commented 1 year ago

Is there a way to force that senario? i only need it on 2 ocasions.

rivantsov commented 1 year ago

hm.. if only on 2 occasions - write method(s) that assign all columns, each column its old value. But there's one more catch - in prop assignment it check if new value is different from old. Play with it and look at SQL log. You might need to do 2 assignments - first some fake different value, then re-assign old value

rivantsov commented 1 year ago

I will look at this in new iteration (making Force-Update-all option), some refactoring is planned in this area

rubenalves commented 1 year ago

Hello, any news on this? when updates was done using stored procedures i never hade problems, but now some times the values are not updated, on 2 ocasions i have to create a second query to the database, alter the value, and save it again, i use one update with vita and another with dapper so i can garantee that the value is altered, the object is in a bindingsource and that probably is the problem, but the workaround is doing the jobe for now.

Thanks.

rivantsov commented 1 year ago

Hi sorry for being late with this and other things, so much stuff happening, changes in life, jobs, tech, world. But getting back to normal, slowly. Use the following method:

    public static void MarkAllColumnsChanged(object entity) {
      var rec = EntityHelper.GetRecord(entity);
      Util.Check(rec != null, "Object is not an entity ({0}) - failed to retrieve entity record.", entity);
      Util.Check(rec.Status == EntityStatus.Loaded || rec.Status == EntityStatus.Modified, 
        "Invalid entity status ({0}) for entity ({1}), must be Loaded or Modified to mark all columns changed.", rec.Status, entity);
      foreach(var m in rec.EntityInfo.Members) {
        var okToSet = m.Kind == EntityMemberKind.Column && !m.Flags.IsSet(EntityMemberFlags.PrimaryKey | EntityMemberFlags.Identity) &&
            rec.ValuesModified[m.ValueIndex] == null;
        if (!okToSet)
          continue;
        // non-null modified value is indicator that value changed and should be updated in database
        rec.ValuesModified[m.ValueIndex] = rec.ValuesOriginal[m.ValueIndex];
      }
      if (rec.Status == EntityStatus.Loaded)
        rec.Status = EntityStatus.Modified;
    }

I will add it to EntityHelper class, that should do it for you

rubenalves commented 1 year ago

It did not resolve, but i have the problem resolved, i save the value, then i execute a new query and compare the value and compare it.