首页 > 解决方案 > 从特定项目运行迁移

问题描述

我有 3 个正在解决的项目。

这是它的样子

在此处输入图像描述

我的迁移在TooSeeWeb.Infrastructure

Startup.csTooSeeWeb 下我有这个代码

 services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection"),
                b => b.MigrationsAssembly("TooSeeWeb.Infrastructure")));

这是 ApplicationDbContext 代码

  public class ApplicationDbContext : IdentityDbContext<AppUser>
{
    public ApplicationDbContext(
        DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<Experience> Experiences { get; set; }
    public DbSet<Proposals> Proposals { get; set; }
    public DbSet<Comments> Comments { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<AppUser> IdentityUsers { get; set; }

    public DbSet<TermsOfUse> TermsOfUse { get; set; }
    public DbSet<Privacy> Privacy { get; set; }


    public DbSet<IdentityUserClaim<string>> IdentityUserClaims { get; set; }
    public DbSet<BookmarkFolder> BookmarkFolders { get; set; }
}

但是当TooSeeWeb.Infrastructure我运行时dotnet ef database update我有这个错误

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

但是我跑的时候dotnet ef database update -s ../TooSeeWeb一切都好。

我该如何解决这个问题?

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

解决方案


你真的必须使用 IdentityDbContext 吗?如果不是,请使用 DbContext 并覆盖 OnConfiguring 方法,以便您可以按如下方式声明连接字符串:

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("connectionString");
        }
    }
}

推荐阅读