postgresql 迁移不工作在dotnet与postgres

ej83mcc0  于 2022-12-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(113)

我正在使用dotnet CLI. IDE - VS代码.
这是一个3层项目

  • SuperMarket.Api.Employees
  • SuperMarket.Data.Employees
  • SuperMarket.Service.Employees.

使用这些Nuget软件包:

Microsoft.EntityFrameworkCore Version=7.0.0,
Microsoft.EntityFrameworkCore.Design Version=7.0.0, 
Npgsql.EntityFrameworkCore.PostgreSQL Version="7.0.0",
Microsoft.EntityFrameworkCore.Tools Version=7.0.0

当我尝试从SuperMarket.Data.Employees层执行此代码时:

dotnet ef migrations add addColoums -s ..\SuperMarket.Api.Employees\

我在终端中收到此错误
实体框架工具版本“6.0.10”比运行库“7.0.0”的版本低。请更新工具以获得最新功能和错误修复。有关详细信息,请参见https://aka.ms/AAc1fbw。访问Microsoft.Extensions.Hosting服务时出错。请在不使用应用程序服务提供程序的情况下继续。错误:动态程序集中不支持调用的成员。无法创建类型为“EmployeeDbContext”的对象。有关设计时支持的不同模式,请参见https://go.microsoft.com/fwlink/?linkid=851728
Program.cs(仅与数据库相关)

var builder = WebApplication.CreateBuilder(args);
builder.Services
    .AddControllers()
.AddFluentValidation (x=>
     { 
      x.ImplicitlyValidateChildProperties = true;
      x.RegisterValidatorsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
     }
    );

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddDbContext<EmployeeDbContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("SuperMarketDb")));

// var logger =  builder.Services.BuildServiceProvider().GetService<ILogger<SuperMarketExceptionMiddleware>>();
// builder.Services.AddSingleton(logger);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
  app.UseSwagger();
  app.UseSwaggerUI();
}
else
{
  app.UseHsts();
}

app.UseStaticFiles();
app.UseCors("corsapp");

app.UseHttpsRedirection();
app.UseMiddleware<SuperMarketExceptionMiddleware>();

app.UseAuthorization();
app.MapControllers();

app.Run();


EmployeeDbContext

using Microsoft.EntityFrameworkCore;
using SuperMarket.Data.Employees.Models;

namespace SuperMarket.Data.Employees.Data
{
    public class EmployeeDbContext : DbContext
    {
        public EmployeeDbContext(DbContextOptions options) : base(options)
        {
        }

        // protected override void OnModelCreating(ModelBuilder modelBuilder)
        // {
        //   modelBuilder.Entity<EmployeeSalary>()
        //     .HasOne<Employee>(x=>x.Employees).WithMany(p=>p.employeeSalary).HasForeignKey;
        // }
        
        public DbSet<Employee> Employees {get; set;}
        public DbSet<User> Users {get; set;}
        public DbSet<SalaryComponents>SalaryComponents {get; set;}
        public DbSet<EmployeeSalary> EmployeeSalary{get; set;}
    }
}

我该如何解决这个问题?我错过了什么吗?
连接字符串正确。

km0tfn4u

km0tfn4u1#

通过使用全局更新ef工具解决问题

dotnet tool update --global dotnet-ef

命令并在做迁移和更新时注解掉下面的部分。后来我取消了注解。

builder.Services
    .AddControllers()
.AddFluentValidation (x=>
     { 
      x.ImplicitlyValidateChildProperties = true;
      x.RegisterValidatorsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
     }
    );

相关问题