I was tried to solve thi problem but until now I can't find the problem. I was search the solution from over internet but still get same error and can't fix it. in many my privious project work fine but now I get this problem.
AppDbContext.cs
using Microsoft.EntityFrameworkCore;
using POS_System.Models;
namespace POS_System.DbCtx
{
public class AppDbContext:DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
{
}
public DbSet<Karyawan> Karyawans { get; set; }
public DbSet<PeranUser> PeranUsers { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<RolePermission> RolePermissions { get; set; }
public DbSet<RoleUser> RoleUsers { get; set; }
public DbSet<Pelanggan> Pelanggans { get; set; }
public DbSet<JenisPelanggan> JenisPelanggans { get; set; }
public DbSet<Kategori> Kategoris { get; set; }
public DbSet<Produk> Produks { get; set; }
public DbSet<ProdukGambar> ProdukGambars { get; set; }
public DbSet<Inventori> Inventoris { get; set; }
public DbSet<Transaksi> Transaksis { get; set; }
public DbSet<AlurKas> AlurKass { get; set; }
public DbSet<MetodeBayar> MetodeBayars { get; set; }
public DbSet<LaporanKeuangan> LaporanKeuangans { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Program.cs
using Microsoft.EntityFrameworkCore;
using POS_System.DbCtx;
using POS_System.Repository;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<IPeranUserRepository, PeranUserRepository>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
the error is
PM> dotnet ef migrations add Init
Build started...
Build succeeded.
Unable to create a 'DbContext' of type 'AppDbContext'. The exception 'Object reference not set to an instance of an object.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
app.MapStaticAssets();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Dashboard}/{action=Index}/{id?}")
.WithStaticAssets();
app.Run();