Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core migrations error using UseInMemoryDatabase

I'm trying to separate my Entity Framework and Identity to a different library but I can't do any migrations when I use builder.UseInMemoryDatabase(connectionString);.

I can do migrations when I change it to builder.UseSqlServer(connectionString);, but I need to use UseInMemoryDatabase.

Here is the error when I try to add migration:

Unable to resolve service for type Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.`

Code:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.InMemory;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;

namespace ClassLibrary1
{
    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
            base.OnModelCreating(modelBuilder);
        }
    }

    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContextFactory()
        {
        }

        public ApplicationDbContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
            var connectionString = configuration.GetConnectionString("DefaultConnection");
            builder.UseInMemoryDatabase(connectionString);

            return new ApplicationDbContext(builder.Options);
        }
    }
}

and this is the reference

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.2.1" />
</ItemGroup>

but the using Microsoft.EntityFrameworkCore.InMemory; is unused namespace.

like image 393
Guest Account Avatar asked Jan 12 '19 02:01

Guest Account


1 Answers

The in-memory concept in meant to simulate your database in your memory (RAM). Migrations are used to generate/update your database schema to the connected database. The in-memory database doesn't need migrations. You can directly start your application and start using your DBContext without trying to add migrations.

As for your confusion with the Microsoft.EntityFrameworkCore.InMemory namespace, you have not written any code that uses the Microsoft.EntityFrameworkCore.InMemory namespace. Note that not every class under a NuGet package is under the namespace. For convenience, the UseInMemoryDatabase extension function is created within the Microsoft.EntityFrameworkCore namespace. This way you don't have to add a using statement each time you change your db.

like image 177
Neville Nazerane Avatar answered Oct 13 '22 11:10

Neville Nazerane