.net core 无法使用dotnet ef migrations add Initial找到DbContext类-s<;项目>-c<;引用项目中的完整类名>-o<;输出方向>;
我试图学习如何使用dotnet核心实体框架生成迁移 我有一个ASP.NET核心Web API项目,它引用了两个项目,每个项目都包含一个DBContext 我已成功地通过以下方式为一个dbcontext生成迁移:.net core 无法使用dotnet ef migrations add Initial找到DbContext类-s<;项目>-c<;引用项目中的完整类名>-o<;输出方向>;,.net-core,entity-framework-core,.net Core,Entity Framework Core,我试图学习如何使用dotnet核心实体框架生成迁移 我有一个ASP.NET核心Web API项目,它引用了两个项目,每个项目都包含一个DBContext 我已成功地通过以下方式为一个dbcontext生成迁移: dotnet ef migrations add Initial -s <startup_project> -c <fully qualified class name from referenced project> -o <output dir>
dotnet ef migrations add Initial -s <startup_project> -c <fully qualified class name from referenced project> -o <output dir>
ASP.NET核心Web API启动项目的项目文件为:
运行时间;建设;本地人;内容文件;分析仪;可传递的
全部的
netcoreapp3.1
找不到上下文类的源代码是:
使用Microsoft.EntityFrameworkCore;
使用Microsoft.EntityFrameworkCore.Metadata.Builders;
命名空间dcs3spp.courseManagementContainers.BuildingBlocks.IntegrationEventLogEF
{
公共类IntegrationEventLogContext:DbContext
{
public IntegrationEventLogContext(DbContextOptions选项):基本(选项)
{
}
公共数据库集集成事件日志{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Entity(ConfigureIntegrationEventLogEntry);
}
无效配置集成事件日志条目(EntityTypeBuilder)
{
builder.ToTable(“集成事件日志”);
HasKey(e=>e.EventId);
builder.Property(e=>e.EventId)
.IsRequired();
builder.Property(e=>e.Content)
.IsRequired();
builder.Property(e=>e.CreationTime)
.IsRequired();
builder.Property(e=>e.State)
.IsRequired();
builder.Property(e=>e.TimesSent)
.IsRequired();
属性(e=>e.EventTypeName)
.IsRequired();
}
}
}
ASP.NET核心Web API项目的Startup.cs添加数据库上下文如下:
services.AddEntityFrameworkNpgsql()
.AddDbContext(选项=>
{
options.UseNpgsql(配置[“ConnectionString”],
NPGSQLPoptions操作:sqlOptions=>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
EnableRetryOnFailure(maxRetryCount:15,maxRetryDelay:TimeSpan.FromSeconds(30),errorCodesToAdd:null);
});
},
ServiceLifetime.Scoped//明确显示在HTTP请求范围内共享DbContext(在HTTP请求中启动的对象图)
);
services.AddDbContext(选项=>
{
options.UseNpgsql(配置[“ConnectionString”],
NPGSQLPoptions操作:sqlOptions=>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
//配置连接弹性:https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
EnableRetryOnFailure(maxRetryCount:15,maxRetryDelay:TimeSpan.FromSeconds(30),errorCodesToAdd:null);
});
});
是否可以从启动项目中为许多DbContext子类生成迁移
如果是这样,找不到DBContext类的潜在原因是什么?问题是,您试图从指向一个数据库的两个不同DBContext创建代码优先数据库-只有一个上下文可以这样做 我建议您创建一个只负责创建数据库的公共DbContext。然后,您可以创建真正的应用程序上下文,其中包含来自大上下文的子集 是否可以从启动项目中为许多DbContext子类生成迁移? 可以为许多DbContext子类生成迁移,以防它们指向不同的数据库 找不到DBContext类的潜在原因是什么? 从您显示的错误中,我看到您正在查找名称空间,而不是当前的DbContext类
“dcs3spp.courseManagementContainers.BuildingBlocks.IntegrationEventLogEF”应为“dcs3spp.courseManagementContainers.BuildingBlocks.IntegrationEventLogEF.IntegrationEventLogContext”通过实现的实例解决了此问题。阅读后找到了这个解决方案 现在当我跑的时候
dotnet ef迁移添加初始-s-c-o
EntityFrameworkCore找到了我的第二个上下文类。+10 Ooops,复制和粘贴错误没有将完整的命名空间复制到问题中……再次尝试以防万一,但仍然找不到上下文类,相同的消息。基于代码的