gpt4 book ai didi

c# - 使用 UseInMemoryDatabase 的 Entity Framework Core 迁移错误

转载 作者:行者123 更新时间:2023-11-30 14:21:36 24 4
gpt4 key购买 nike

我试图将我的 Entity Framework 和身份分离到不同的库,但是当我使用 builder.UseInMemoryDatabase(connectionString); 时我无法进行任何迁移。

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

这是我尝试添加迁移时的错误:

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

代码:

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);
}
}
}

这是引用

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

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

最佳答案

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

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

关于c# - 使用 UseInMemoryDatabase 的 Entity Framework Core 迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com