C# 使用UseInMemoryDatabase时实体框架核心迁移错误

C# 使用UseInMemoryDatabase时实体框架核心迁移错误,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我试图将我的实体框架和标识分离到另一个库,但当我使用builder.UseInMemoryDatabase(connectionString)时,我无法进行任何迁移 当我将其更改为builder.UseSqlServer(connectionString)时,我可以进行迁移,但我需要使用UseInMemoryDatabase 以下是我尝试添加迁移时出现的错误: 无法解析Microsoft.EntityFrameworkCore.Migrations.IMigrator'类型的服务。这通常是因为没

我试图将我的实体框架和标识分离到另一个库,但当我使用
builder.UseInMemoryDatabase(connectionString)时,我无法进行任何迁移

当我将其更改为
builder.UseSqlServer(connectionString)时,我可以进行迁移,但我需要使用
UseInMemoryDatabase

以下是我尝试添加迁移时出现的错误:

无法解析Microsoft.EntityFrameworkCore.Migrations.IMigrator'类型的服务。这通常是因为没有为此DbContext配置数据库提供程序。可以通过重写DbContext.OnConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用AddDbContext,那么还要确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数`

代码:

使用Microsoft.AspNetCore.Identity;
使用Microsoft.AspNetCore.Identity.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore.Design;
使用Microsoft.EntityFrameworkCore.InMemory;
使用Microsoft.Extensions.Configuration;
使用制度;
使用System.IO;
命名空间类库1
{
公共类应用程序用户:IdentityUser
{
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
//modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext.Assembly));
基于模型创建(modelBuilder);
}
}
公共类ApplicationDbContextFactory:IDesignTimeDbContextFactory
{
公共应用程序dbContextFactory()
{
}
公共应用程序DBContext CreateDbContext(字符串[]args)
{
IConfigurationRoot配置=新配置生成器()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”)
.Build();
var builder=new DbContextOptionsBuilder();
var connectionString=configuration.GetConnectionString(“DefaultConnection”);
builder.UseInMemoryDatabase(connectionString);
返回新的ApplicationDbContext(builder.Options);
}
}
}
这是参考资料

<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>


但是
使用Microsoft.EntityFrameworkCore.InMemory
是未使用的命名空间。

中的内存中概念旨在模拟内存中的数据库(RAM)。迁移用于生成/更新连接数据库的数据库架构。内存中的数据库不需要迁移。您可以直接启动应用程序并开始使用DBContext,而无需尝试添加迁移


至于您对
Microsoft.EntityFrameworkCore.InMemory
命名空间的混淆,您还没有编写任何使用
Microsoft.EntityFrameworkCore.InMemory
命名空间的代码。请注意,并非NuGet包下的每个类都在名称空间下。为方便起见,在
Microsoft.EntityFrameworkCore
命名空间中创建了
UseInMemoryDatabase
扩展函数。这样,您就不必在每次更改数据库时添加using语句

为什么要在使用inmemory提供程序的过程中运行迁移?因为我不想在开发过程中处理sqlserver。localdb是一个选项。内存是一个短期的东西,没有迁移。哦,我只是想如果我要在内存数据库中使用,我需要一个不同的迁移模式
<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>