1

I have an issue with a model not updating to the latest version after requiring the database (probably due to change tracking?).

I add the model to the dbset and save it through the context. At this point there is a server side trigger that updates a field with a link to another table pk column.

After adding the record i need to know the value in the updated column. I requery the database, and a SQL Server trace shows that the new value is returned, but the model value is not updated.

I have tried dethatching the entities similar to this answer. but this does not work.

I have worked around it by creating a new scope from the service scope factory and creating a second instance of my context, but would like to know why this happens and how to avoid it (I have no control of the database so I can't remove the trigger and do everything in code sadly).

Thanks

Paul

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul
  • 693
  • 1
  • 10
  • 25
  • 1
    It's possible to refresh the cached object instance (or use no tracking query) to get the updated value, but isn't it better in your case to configure the property as [Value generated on add or update](https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations#value-generated-on-add-or-update) (the documentation example is showing trigger usage similar to yours)? This way you won't need manual requery, EFC will automatically do that for you. – Ivan Stoev Nov 27 '20 at 11:24
  • Thanks thats exactly what I was looking for! – Paul Nov 27 '20 at 12:27

1 Answers1

1

Ivans answer in the comments takes care of this. anotating the property in the model with [DatabaseGenerated(DatabaseGeneratedOption.Computed)]

takes care of everything atuomatically.

Thanks for the help

Paul
  • 693
  • 1
  • 10
  • 25