.netsqliteunit-testingin-memory-database

Work with Concurrency Tokens shadow column with InMemory database in .NET


I'm trying to create unit tests for the database repository class using the InMemory Sqlite database. I have one of the entities with the Concurrency Tokens column named xmin.
enter image description here

This column configured in DbContext this way:
enter image description here

And then I'm trying to setup in memory context for unit tests: enter image description here

In TestDataBuilder I've filled this column with some value. Here's an error I'm getting: enter image description here

enter image description here


Solution

  • After a year I've got the same issue again and was able to find a solution.

    For setting up in-memory DB I've created a new DB Context derived from original one and override settings for xmin field:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<PgIdentity>().Property(i => i.xmin).HasDefaultValue((uint)DateTime.UtcNow.Ticks);
        }
    

    This way I can override some DB settings that we don't need (or not support) in InMemory database.

    Hope it will be helpful for someone.