dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.73k stars 3.17k forks source link

Improve the experience for using SQLite in-memory databases #16103

Open ajcvickers opened 5 years ago

ajcvickers commented 5 years ago

For example, some sugar to make it easier to keep a connection open across multiple context instances.

@divega @bricelam I believe you guys have some additional thoughts here--maybe link some of the information from Brice's blog?

ArthurHNL commented 4 years ago

Maybe also document which features need to be implemented "manually", for example I had to specify a standard default value for concurrency tokens, a converter for concurrency tokens, a comparer for concurrency tokens and a converter for datetimeoffssets

musicm122 commented 4 years ago

Documentation on Registering with Dependency Injection and maintaining\ re-hydrating state across multiple tests would be useful as well.

Timovzl commented 4 years ago

I only just spotted this and haven't tried it, but perhaps it's made for this scenario: Sharable in-memory

Data Source=Sharable;Mode=Memory;Cache=Shared
Ninjanaut commented 3 years ago

It's probably obvious, but there is no mention which Nuget package to install if someone want to use EF Sqlite in-memory feature.

ajcvickers commented 3 years ago

@Ninjanaut It doesn't require a different NuGet package; it's a feature built in to SQLite.

sguryev commented 3 years ago

I have created method in base test class: protected AppDbContext GetDbContext() => new AppDbContext(_dbContextOptions); So any time I need new context I call it.

public async Task HasAccessAsync()
{
    var dbContext = GetDbContext();
    var users = dbContext.Users.ToList();
    var service = dbContext.Services.Include(s => s.Users).Include(s => s.Groups).First();
    Assert.True(await _service.HasAccessAsync<Service>(users[0].Id, service.Id));
    Assert.True(await _service.HasAccessAsync<Service>(users[1].Id, service.Id));
    Assert.False(await _service.HasAccessAsync<Service>(users[2].Id, service.Id));
}

I have came up to the statement that I need fresh context most of the time to avoid caching in Local and pass missing .Include() calls. Are there some more complicated connection management tasks?

Yaevh commented 3 years ago

the documentation (https://docs.microsoft.com/en-us/ef/core/testing/sqlite) is IMHO misleading re using SQLite in-memory database in conjunction with .AddDbContext() method, see #25204

user1568891 commented 3 years ago

It's probably obvious, but there is no mention which Nuget package to install if someone want to use EF Sqlite in-memory feature.

I agree. For someone new to sqlite just doing a Nuget Package Manager search for sqlite returns a lot of results. It really wouldn't hurt to note that Microsoft.EntityFrameworkCore.Sqlite is required.