Entity Framework Core - In Memory Provider Required Field

effort entity-framework-core


In previous versions of Entity Framework I have been using Effort (https://effort.codeplex.com/) for unit tests. I decided to give the new in memory provider for EF Core and quickly found out that it does not honor the IsRequired() and other entity configurations set in OnModelCreating. Is there a way to make it honor this configuration? If not, is this on a todo list to be implemented? Maybe even an alternative in memory provider?

I would like to be able to use the test steps to swap out the context and use a real db in some integration test scenarios that could leverage the same code. This seems like a "nice to have", and maybe that is a case for effort with EF Core. I couldn't find anything about effort being worked on for EF Core yet either.

I couldn't find anything on the Uservoice page for EF (https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions) and will go there next if it just isn't available.

2/12/2016 2:05:26 AM

Popular Answer

This question was asked quite a while back, but I had the same question and stumbled across this unanswered question. I ended up finding an answer that works for me so I thought I'd share in case someone else run across this like I did.

It appears that SQLite has an in memory option that acts more like a real database than the standard UseInMemory() option: https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/sqlite

Only difference was I added Microsoft.EntityFrameworkCore.Sqlite, not Microsoft.Data.Sqlite like the docs say.

Not completely relevant to the question but in my particular case I am wanting to use an in memory while I'm testing the basic functionality of a webapi I'm working on but I still want to be able to test unique constraints, etc. Here is what I added to implement the in memory database.

Within Configure Services:

services.AddDbContext<MyDbContext>(options =>
    var liteConn = new SqliteConnection("DataSource=:memory:");

        .ConfigureWarnings(warnings =>

Within Configure:

if (env.IsDevelopment())
    var context = app.ApplicationServices.GetRequiredService<MyDbContext>();
8/13/2017 2:43:23 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow