簡體   English   中英

在 ASP.NET Core 中的 UseInMemoryDatabase 和標識中創建數據庫

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

我需要在 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);
    }
}

我怎么解決這個問題?

為什么需要對內存數據庫進行遷移?

遷移用於對數據庫進行版本控制。 當您在生產中擁有一個數據庫,您更改了 smth 並且您可以使用遷移更新它時,它很有用。 沒有必要將它用於內存數據庫。 您可以在此處閱讀有關遷移的更多信息。

無論如何, InMemory提供程序僅設計用於單元和集成測試。 如果你想在開發和生產中使用內存,你應該使用SQLite 這是問題哦, github

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM