【问题标题】:Create database in UseInMemoryDatabase in ASP.NET Core and identity在 ASP.NET Core 和标识的 UseInMemoryDatabase 中创建数据库
【发布时间】:2020-06-04 12:06:32
【问题描述】:

我需要在 ASP.NET Core 中创建一个数据库并使用身份,并且我需要使用 UseInMemoryDatabase 但是当我尝试添加迁移时,我得到了这个错误:

无法创建“DortajContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

这是我的开始:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContextPool<DortajContext>(options => options.UseInMemoryDatabase(databaseName: "DortajDistance"));
        services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<DortajContext>();
        services.AddControllers();
    }

这是我的上下文:

 public class DortajContext : IdentityDbContext<User, Role, int>
{
    public DortajContext(DbContextOptions<DortajContext> options)
  : base(options)
    {
    }

    public DbSet<UserDistanceHistory> UserDistanceHistories { get; set; }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ApplyConfigurationsFromAssembly(typeof(IType).Assembly);
    }
}

我该如何解决这个问题?

【问题讨论】:

    标签: c# asp.net database asp.net-core


    【解决方案1】:

    为什么需要迁移内存数据库?

    迁移用于版本控制数据库。当您在生产中拥有数据库时,它很有用,您更改了 smth 并且可以通过迁移对其进行更新。无需将其用于内存数据库。您可以阅读有关迁移的更多信息here

    无论如何,InMemory 提供程序仅设计用于单元和集成测试。如果你想在开发和生产中使用内存,你应该使用SQLite。这是问题哦github

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多