I am trying to add migartion to my database i keep having this error.Please i am a rookie to MVC.My code
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace DemoRepository.Model
{
public class ProdDBContext : DbContext
{
public DbSet<Worker> Workers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\EMMA;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;");
}
base.OnConfiguring(optionsBuilder);
}
public void ConfigureServices(IServiceCollection services)
{
IServiceCollection serviceCollection = services.AddDbContext<ProdDBContext>(
options => options.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;"));
}
}
}
Error Message
Unable to create a 'DbContext' of type ''. The exception 'No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Моя проблема в основном заключается в том, что я не могу добавить миграцию в базу данных. Я продолжаю получать сообщение об ошибке, как указано выше. Я установил ядро Entityframework, Entityframework, sql и, наконец, Entityframework.Tools. Пожалуйста, помогите мне.
public DbSet<Worker> Workers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;");
}
base.OnConfiguring(optionsBuilder);
}
}
В файле Program.cs добавьте приведенный ниже код.
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
services.AddDbContextPool<ProdDBContext>(o => o.UseSqlServer(builder.Configuration["ConnectionStrings:DefaultConnection"]));
Файл appsettings.json выглядит так
{
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Ошибка ясно говорит
No database provider has been configured for this DbContext
. Правильно ли вы настроили DbContext перед попыткой добавления миграции?