Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core - In Memory Provider Required Field

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.

like image 271
Matt Sanders Avatar asked Feb 12 '16 02:02

Matt Sanders


People also ask

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

Does EF core cache data by default?

The Cache: The memory cache is used by default. The Cache Key: The cache key is created by combining a cache prefix, all cache tags and the query expression. The Query Materialized: The query is materialized by either using "ToList()" method or "Execute()" method for query deferred.

How do you use UseInMemoryDatabase?

Inject ApiContext to the CategoryController via constructor. Create a GET method which will call GetCategories of ApiContext and return the data. Configure ApiContext in the startup class with the option UseInMemoryDatabase. Now, run the application and you will be able to see all the added categories.


1 Answers

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:");
    liteConn.Open();

    options
        .UseSqlite(liteConn)
        .ConfigureWarnings(warnings =>
        {
            warnings.Throw(RelationalEventId.QueryClientEvaluationWarning);
            warnings.Log(RelationalEventId.ExecutedCommand);
        });
});

Within Configure:

if (env.IsDevelopment())
{
    var context = app.ApplicationServices.GetRequiredService<MyDbContext>();
    context.Database.EnsureCreated();
}
like image 79
SolidSloth Avatar answered Oct 06 '22 22:10

SolidSloth