Не могу провести миграцию БД EF при наличии 2-х проектов в решение
Есть 2 проекта в одно Решение: Библиотека Классов и Web API
DBLib\LibContext.cs
using DBLib.Models;
using Microsoft.EntityFrameworkCore;
namespace DBLib
{
public class LibContext : DbContext
{
public LibContext(DbContextOptions<LibContext> option) : base(option) { }
public DbSet<User> Users { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Post>()
.HasOne(p => p.Author)
.WithMany(u => u.Posts)
.HasForeignKey(p => p.UserId);
modelBuilder.Entity<Post>()
.HasOne(p => p.Category)
.WithMany(c => c.Posts)
.HasForeignKey(p => p.CategoryId);
}
}
}
EDU_API\Program.cs
using DBLib;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<LibContext>(option =>
option.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Вывод в Консоль диспетчера пакетов:
Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[DBLib.LibContext]' while attempting to activate 'DBLib.LibContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728