c# - EF Core 迁移似乎忽略了上下文选项
问题描述
在将身份添加到我的项目中时,我正在通过第二个上下文这样做。
但是,在运行以下命令时
dotnet ef migrations add IdentityInitial -p .\DOH.Data -s .\DOH.API
-c AppIdentityDbContext -o DOH.Data\Identity\Migrations --verbose
我得到以下输出
Using assembly 'DOH.Data'.
Using startup assembly 'DOH.API'.
Using application base 'C:\Users\vic\Documents\projects\BugTracker\DOH\DOH.API\bin\Debug\netcoreapp3.1'.
Using working directory 'C:\Users\vic\Documents\projects\BugTracker\DOH\DOH.API'.
Using root namespace 'DOH.Data'.
Using project directory 'C:\Users\vic\Documents\projects\BugTracker\DOH\DOH.Data\'.
Remaining arguments: .
The Entity Framework tools version '5.0.1' is older than that of the runtime '5.0.5'. Update the tools for the latest features and bug fixes.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'DOH.API'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'ApplicationContext'.
Found DbContext 'AppIdentityDbContext'.
Finding DbContext classes in the project...
Using context 'AppIdentityDbContext'.
System.InvalidOperationException:实体类型“PersonApplicationRole”需要定义主键。如果您打算使用无密钥实体类型,请在“OnModelCreating”中调用“HasNoKey”。有关无密钥实体类型的详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2141943。
该实体PersonApplicationRole
是在 中引用的现有实体ApplicationContext
。
该实体已通过先前使用复合键的迁移在数据库中设置,因此我知道该部分有效
如果我更改实体定义以处理错误只是为了通过身份迁移,我最终会得到一个迁移,其中包含引用的所有原始实体ApplicationContext
以及需要创建的表作为身份集的一部分向上。
所以这感觉就像这个-c
选项被忽略了。
这是 的定义ApplicationContext
:
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationContext>
{
public ApplicationContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../DOH.API/appsettings.Development.json").Build();
var builder = new DbContextOptionsBuilder<ApplicationContext>();
var connectionString = configuration.GetConnectionString("doh.dev");
builder.UseNpgsql(connectionString);
return new ApplicationContext(builder.Options);
}
}
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options)
{ }
}
这是当前的AppIdentityDbContext
:
public class AppIdentityDbContext : IdentityDbContext<AppUser>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
这是两个连接字符串
"ConnectionStrings": {
"doh.dev": "Host=x.x.x.x;Port=5432;Username=dohdbsvc;Password=******;Database=dohdb;",
"doh.dev.identity": "Host=x.x.x.x;Port=5432;Username=dohdbsvc;Password=******;Database=doh.identitydb;"
},
这是我的启动中包含的两个上下文
services.AddDbContext<ApplicationContext>(options =>
{
options.EnableDetailedErrors();
options.UseNpgsql(
Configuration.GetConnectionString("doh.dev"));
});
services.AddDbContext<AppIdentityDbContext>(options =>
{
options.EnableDetailedErrors();
options.UseNpgsql(
Configuration.GetConnectionString("doh.dev.identity"));
});
作为参考,这是一个使用以下包的 .net core 3.1 项目
Microsoft.AspNetCore.Identity.EntityFrameworkCore 5.0.5
Microsoft.AspNetCore.Identity 2.2.0
Microsoft.EntityFrameworkCore 5.0.5
Microsoft.EntityFrameworkCore.Design 5.0.5
Npgsql.EntityFrameworkCore.PostgreSQL 5.0.2
Microsoft.EntityFrameworkCore.Tools 5.0.5
我浏览了其他人在各种博客和论坛上发布的文档和无数示例,我似乎正在遵循推荐的做法,所以如果有人能明白为什么会发生这种情况,请分享你的想法。
解决方案
推荐阅读
- react-intl - 是否可以在两种货币单位之间切换?
- sql - 使用 SQL 更新具有 XML 的表中的特定列
- python - Python Tkinter 标签更新?
- javascript - 在 vis.js 中选择动画边缘颜色变化
- google-cloud-platform - Google 云合规性检查(最佳做法)
- python - Coremltools 使用 Keras 将 Tensorflow 模型转换为 Core ML 的问题
- javascript - Vanilla JS 迭代 div innerHTML 以便以设定的间隔显示和隐藏
- json - 在 Angular 2+ 中将 json 数组转换为服务中的 json 对象
- python-3.x - 在 Python 中迭代字典的简洁方法
- wordpress - 文件中没有循环的 Wordpress 标题,如 single.php