0

I am getting the error when trying to run my application. Using .NET Core on Rider for Mac.

Program.cs

using System.Configuration;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MoneyTracker.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true);
builder.Services.AddControllersWithViews();
//Allows us to connect to our database using the connection string in Program.cs 
//Uses the ApplicationDbContext file under Data to invoke the table creation
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("SolutionConnection")
)); 
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    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.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

app.Run();

My ApplicationDbContext.cs

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using MoneyTracker.Models;

namespace MoneyTracker.Data;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, DbSet<solutionDb> solutionDb)
        : base(options)
    {
    }
    
    public DbSet<solutionDb> SolutionDb { get; set; } //creates a solution table with the name solutionDb
}

my solutionDb.cs Model file

using System.ComponentModel.DataAnnotations; 
using System.ComponentModel;

namespace MoneyTracker.Models;


public class solutionDb

{
    [Key] 
    public int Id { get; set; }
    [Required] 
    public string SolutionName {get; set;} = null!;
    public int Cost {get; set;}
}

I tried going back through and retracing steps, but I can't figure out what I am doing wrong here. I attempted restructure the Program.cs file and refresh the DB connection running through Azure SQL Edge. I am newer to C# and the MVC style, so apologies if this is a simple fix.

Drizzmo
  • 1
  • 1

1 Answers1

0

Don't write DbSet<solutionDb> solutionDb in constructor of ApplicationDbContext. Asp.Net Core using Constructor injection, When you write DbSet<solutionDb> solutionDb in ApplicationDbContext's constructor, The project will think that you inject DbSet<solutionDb> solutionDb into ApplicationDbContext, But you don't register it in DI container, So project will report this error, Change your code like:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    
    public DbSet<solutionDb> SolutionDb { get; set; } //creates a solution table with the name solutionDb
}

You can refer to this Docs to learn more details.

Xinran Shen
  • 8,416
  • 2
  • 3
  • 12