我正在尝试学习如何使用dotnet核心Entity Framework生成迁移。
我有一个ASP.NET Core Web API项目,该项目引用了两个项目,每个项目都包含一个DBContext。
我已成功使用以下方法成功为一个dbcontext生成了迁移:
dotnet ef migrations add Initial -s <startup_project> -c <fully qualified class name from referenced project> -o <output dir>
但是,对于第二个DbContext,它找不到包含以下消息的DbContext:
No DbContext named 'dcs3spp.courseManagementContainers.BuildingBlocks.IntegrationEventLogEF' was found.
所引用项目的项目文件具有正确引用了内容的路径:
<Project Sdk="Microsoft.NET.Sdk"> <ItemGroup> <ProjectReference Include="..\EventBus\EventBus.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.1" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.1.2" /> </ItemGroup> <PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> </PropertyGroup> </Project>
ASP.NET Core Web API启动项目的项目文件是:
<Project Sdk="Microsoft.NET.Sdk.Web"> <ItemGroup> <ProjectReference Include="..\Courses.Domain\Courses.Domain.csproj" /> <ProjectReference Include="..\Courses.Infrastructure\Courses.Infrastructure.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\IntegrationEventLogEF\IntegrationEventLogEF.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\WebHost.Customization\WebHost.Customization.csproj" /> <ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" /> </ItemGroup> <ItemGroup> <PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="3.0.0" /> <PackageReference Include="AspNetCore.HealthChecks.Npgsql" Version="3.0.0" /> <PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="3.0.5" /> <PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="3.0.2" /> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Dapper" Version="2.0.30" /> <PackageReference Include="MediatR" Version="8.0.0" /> <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1"> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include="Npgsql" Version="4.1.3" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.1.2" /> <PackageReference Include="Polly" Version="7.2.0" /> <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" /> <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" /> <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> <PackageReference Include="Serilog.Sinks.Http" Version="5.2.0" /> <PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" /> <PackageReference Include="System.Reflection" Version="4.3.0" /> </ItemGroup> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> </Project>
找不到的上下文类的源代码是:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace dcs3spp.courseManagementContainers.BuildingBlocks.IntegrationEventLogEF { public class IntegrationEventLogContext : DbContext { public IntegrationEventLogContext(DbContextOptions<IntegrationEventLogContext> options) : base(options) { } public DbSet<IntegrationEventLogEntry> IntegrationEventLogs { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<IntegrationEventLogEntry>(ConfigureIntegrationEventLogEntry); } void ConfigureIntegrationEventLogEntry(EntityTypeBuilder<IntegrationEventLogEntry> builder) { builder.ToTable("IntegrationEventLog"); builder.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(); builder.Property(e => e.EventTypeName) .IsRequired(); } } }
ASP.NET Core Web API项目的Startup.cs添加数据库上下文如下:
services.AddEntityFrameworkNpgsql() .AddDbContext<CourseContext>(options => { options.UseNpgsql(configuration["ConnectionString"], npgsqlOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorCodesToAdd: null); }); }, ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request) ); services.AddDbContext<IntegrationEventLogContext>(options => { options.UseNpgsql(configuration["ConnectionString"], npgsqlOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorCodesToAdd: null); }); });
是否有可能从启动项目中为许多DbContext子类生成迁移?
如果是这样,导致无法找到DBContext类的潜在原因是什么?
我正在尝试学习如何使用dotnet核心Entity Framework生成迁移。我有一个ASP.NET Core Web API项目,该项目引用了两个项目,每个项目都包含一个DBContext。我有...
问题是,您试图从指向一个数据库的两个不同的DbContext创建一个代码优先的数据库-只有一个上下文可以做到这一点。