I am using ef-core 2.2 in connection with sqlite. In order to prevent/catch concurrency exceptions, I am creating a concurrency token in the according table. The token is a timestamp which I am configuring in my DbContext
class via Fluent API:
modelBuilder.Entity<User>().Property( u => u.Timestamp ).IsRowVersion();
After that I need the timestamp to be updated on every update to my table-entry, therefor I am appending a sql statement to my automatically created migration file:
In Up( MigrationBuilder migrationBuilder )
:
migrationBuilder.Sql( "CREATE TRIGGER SetUserTimestamp AFTER UPDATE ON Users BEGIN UPDATE Users SET Timestamp = randomblob( 8 ) WHERE Id = NEW.Id; END" );
In Down( MigrationBuilder migrationBuilder )
I append:
migrationBuilder.Sql( "DROP TRIGGER SetUserTimestamp" );
This solution does work well. One problem for me is that I have to be extremely careful not to forget including the trigger creation after I created a new migration via command line. Is there any way to configure this trigger in the DbContext
class, maybe via Fluent API? Or is there another way to automate the creation of the trigger?