Я создаю приложение для тестирования PostGreSQL с. NET Core 3.1 на компьютере Ma c. На моем слое WebApi я добавляю DBContext моей базы данных. Это мой startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkNpgsql();
services.AddDbContext<StudioReservationContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("PostgreSqlConnectionString")));
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
И в моем классе DbContext у меня есть следующий код:
public class StudioReservationContext : DbContext
{
//on the base part will go the connection string
public StudioReservationContext(DbContextOptions<StudioReservationContext> options) : base(options)
{
}
public StudioReservationContext()
{
}
public DbSet<Client> Client { get; set; }
public DbSet<Payment> Payment { get; set; }
public DbSet<Reservation> Reservation { get; set; }
public DbSet<Studio> Studio { get; set; }
public DbSet<StudioRoom> StudioRoom { get; set; }
public DbSet<StudioRoomSchedule> StudioRoomSchedule { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
//this is necessary, because EF Core, doesn`t have the method "Conventions"
//that we use to make the configuration of the tables
foreach (var item in modelBuilder.Model.GetEntityTypes())
{
item.SetTableName(item.DisplayName().ToString());
}
}
}
Моя главная проблема - когда я пытаюсь создать свою первую миграцию, запустив Команда "do tnet ef migrations add FirstMigration" возвращает мне исключение, говорящее:
System.InvalidOperationException: для этого DbContext не настроен поставщик базы данных. Поставщик может быть настроен путем переопределения метода DbContext.OnConfiguring или с помощью AddDbContext в поставщике службы приложений. Если используется AddDbContext, то также убедитесь, что ваш тип DbContext принимает объект DbContextOptions в своем конструкторе и передает его базовому конструктору для DbContext
Дело в том, что я передаю провайдера PostGreSQL на моем startup.cs используя DI. И я получаю это на моем классе DBContext, поэтому я не знаю точно, что происходит в этой ситуации