18

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.

3
  • 1
    why are you running migrations as part of using the inmemory provider? Commented Jan 12, 2019 at 3:04
  • because i dont want to deal with sqlserver during development. Commented Jan 12, 2019 at 3:25
  • theres localdb which is an option. in memory is a short term thing that doesn't have migrations. Commented Jan 12, 2019 at 3:27

1 Answer 1

32

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ohh, i just thought i need a different migration schema if i were to use inmemorydatabase

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.