Contents

Use Sqlite Instead of Db in Memory to Verify Foreign Key Constrains

When we need to check check, we need to use *Sqlite provider*

Contents

How to check check Constrains in test ?

Todo: i need to insert some description :D

When we need to check check, we need to use Sqlite provider

/2020/06/02/use-sqlite-instead-of-db-in-memory-to-verify-foreign-key-constrains/when_use_what_providers.png
When use waht providers
using System;
using System.Threading.Tasks;
using Bogus;
using Microsoft.EntityFrameworkCore;
using TrackingConsumerService.Data;
using TrackingConsumerService.Data.Models;
using Xunit;

namespace TrackingConsumerService.Test.Fixture
{
    public class DbContextFixture : IDisposable
    {
        public MyDbContest DbContext { get; private set; }


        public DbContextFixture()
        {
            var options = new DbContextOptionsBuilder<MyDbContest>()
                .UseSqlite("Filename=:memory:")
                .Options;
            DbContext = new MyDbContest(options);
            DbContext.Database.OpenConnection();
            DbContext.Database.EnsureCreated();
        }
        public void Dispose()
        {
            DbContext?.Database?.CloseConnection();
            DbContext?.Dispose();
        }
    }

    [CollectionDefinition("DbContext")]
    public class InMemoryDbContextFixtureCollection : ICollectionFixture<DbContextFixture>
    {
        // This class has no code, and is never created. Its purpose is simply
        // to be the place to apply [CollectionDefinition] and all the
        // ICollectionFixture<> interfaces.
    }
}        

todo: add test example