Hanna Holasava
Hanna Holasava

Reputation: 210

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

Upvotes: 0

Views: 126

Answers (2)

Hanna Holasava
Hanna Holasava

Reputation: 210

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.

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76414

The error message is telling you that

NOT NULL constraint failed

which happens upon saving null for not nullable values. You will need to find out what specific value/field is null and make sure that you will save valid data.

Upvotes: 0

Related Questions