简体   繁体   English

在 ASP.NET Core 中的 UseInMemoryDatabase 和标识中创建数据库

[英]Create database in UseInMemoryDatabase in ASP.NET Core and identity

I need to create a database in ASP.NET Core and use identity, and I need to use UseInMemoryDatabase but when I try to add a migration, I get this error:我需要在 ASP.NET Core 中创建一个数据库并使用身份,我需要使用UseInMemoryDatabase但是当我尝试添加迁移时,出现此错误:

Unable to create an object of type 'DortajContext'.无法创建类型为“DortajContext”的对象。 For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728

This is my start up:这是我的启动:

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

and this is my context :这是我的背景:

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

How can I solve this problem?我怎么解决这个问题?

Why do you need migrations for in-memory database?为什么需要对内存数据库进行迁移?

Migrations are used to versioning database.迁移用于对数据库进行版本控制。 It is useful when you have a database in production, you changed smth and you can update it with migrations.当您在生产中拥有一个数据库,您更改了 smth 并且您可以使用迁移更新它时,它很有用。 There is no need to use it for in-memory databases.没有必要将它用于内存数据库。 You can read more about migrations here .您可以在此处阅读有关迁移的更多信息。

Anyway, InMemory provider was designed only for unit and integration testing.无论如何, InMemory提供程序仅设计用于单元和集成测试。 If you want to use in-memory in developing and production, you should use SQLite .如果你想在开发和生产中使用内存,你应该使用SQLite Here is issue oh the github这是问题哦, github

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM