简体   繁体   中英

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:

Unable to create an object of type 'DortajContext'. For the different patterns supported at design time, see 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. 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. If you want to use in-memory in developing and production, you should use SQLite . Here is issue oh the github

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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