0
votes

I am trying to configure a MVP for a reproduceable repo but for that I am using sql lite so it can be run without configuration, however in that I am getting an error in it self. I am using this repo as a way to demonstrate that something else is not working however I dont get why I am getting that?

My Context is :

namespace AuthorisationRepoMS.Dal {
public class ApplicationDBContext : IdentityDbContext<ApplicationUser> {

      public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
        : base(options) {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);

    }

    }
}

My Startup.cs is as follows

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {              
        services.AddEntityFrameworkSqlite().AddDbContext<ApplicationDBContext>();
        services.AddIdentity<ApplicationUser, IdentityRole>()
   .AddDefaultUI()
   .AddRoles<IdentityRole>()
   .AddDefaultTokenProviders()
   .AddEntityFrameworkStores<ApplicationDBContext>().AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();  //<---- add this line 
        services.AddControllersWithViews()
        .AddDataAnnotationsLocalization();
        services.Configure<CookiePolicyOptions>(options => {
            options.CheckConsentNeeded = context => true; // consent required
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddSession(opts => {
            opts.Cookie.IsEssential = true; // make the session cookie Essential
        });
        services.AddControllers(config => {
            // using Microsoft.AspNetCore.Mvc.Authorization;
            // using Microsoft.AspNetCore.Authorization;
            var policy = new AuthorizationPolicyBuilder()
                             .RequireAuthenticatedUser()
                             .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });
        services.Configure<IdentityOptions>(options => {
            // Default Lockout settings.
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;
        });
        services.AddTransient<MISDBContextSeedData>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    }

But I am getting the following Error

System.InvalidOperationException: '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 object in its constructor and passes it to the base constructor for DbContext.'

Thanks in advance

1

1 Answers

1
votes

You need to properly configure your database context. If you are using SQLite, then it should looke something like this:

services.AddDbContext<ApplicationDBContext>(options =>
    options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

This will assume that you have a connection string properly defined, e.g. "Data Source=database.db" for SQLite.