c# - 从特定项目运行迁移
问题描述
我有 3 个正在解决的项目。
这是它的样子
我的迁移在TooSeeWeb.Infrastructure
在Startup.cs
TooSeeWeb 下我有这个代码
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
一切都好。
我该如何解决这个问题?
解决方案
你真的必须使用 IdentityDbContext 吗?如果不是,请使用 DbContext 并覆盖 OnConfiguring 方法,以便您可以按如下方式声明连接字符串:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("connectionString");
}
}
}
推荐阅读
- android - Android 应用内结算货币代码始终为美元
- pyspark - Pyspark 管道自定义变压器
- javascript - Array.sort() 比较函数返回未定义
- dask - Dask 不尊重工人指令/约束 (workers='...', allow_other_workers=False)
- typescript - Angular subscribe 丢失了实际的类类型
- pandas - 根据条件计算数据框中的连续出现次数
- python - 如何使用 Python 的 Pyperclip 将文本粘贴到单独的程序上?
- javascript - 检查先前的值是否已更改,如果已更改,请将更改前的先前值放入日志表中并在屏幕上打印该日志表
- tensorflow - 要建立什么样的神经网络模型来识别算法是 n*n?
- apache - 如何解释 apache mod 重写规则?