0

Recently I wanted to implement authentication into my C# MVC web application and I started following the guide from Claim based authorization design for conditional edit operation in ASP.NET MVC App. However, I ran into an issue when it came to my DbContext class.

I cannot seem to make it work without it giving multiple errors. If I resolve one of them another one breaks. I tried looking it up, but cannot seem anyone that ran into the same issue as I did.

I don't get why it breaks since the classes it inherits from now still inherit from DbContext, there's just 2 classes inbetween.

CODE WITH CLASS IMPLEMENTEING DbContext, THIS CODE WORKS BUT HAS TO BE CHANGED

using Microsoft.EntityFrameworkCore;
using ProjectKiwi.Data.Mappers;
using ProjectKiwi.Models.Domain;

namespace ProjectKiwi.Data
{
    public class ApplicationDbContext : DbContext
    {
        public DbSet<Nieuwsartikel> Artikels { get; set; }
        public DbSet<Bestuurslid> Bestuurleden { get; set; }
        public DbSet<Locatie> Locaties { get; set; }
        public DbSet<ContactPagina> ContactPaginas { get; set; }
        public DbSet<Spreker> Sprekers { get; set; }
        public DbSet<Seminarie> Seminaries { get; set; }
        public DbSet<Categorie> Categories { get; set; }
        public DbSet<Deelnemer> Deelnemers { get; set; }
        public DbSet<Inschrijving> Inschrijvingen { get; set; }
        public DbSet<Organisatie> Organisaties { get; set; }
        public DbSet<Forumpost> Forumposts { get; set; }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        { //I really need these configurations in my new app so I can choose how mapping is done.
            base.OnModelCreating(builder);
            builder.ApplyConfiguration(new ArtikelConfiguration());
            builder.ApplyConfiguration(new BestuurslidConfiguration());
            builder.ApplyConfiguration(new LocatieConfiguration());
            builder.ApplyConfiguration(new ContactPaginaConfiguration());
            builder.ApplyConfiguration(new CategorieConfiguration());
            builder.ApplyConfiguration(new DeelnemerConfiguration());
            builder.ApplyConfiguration(new InschrijvingConfiguration());
            builder.ApplyConfiguration(new OrganisatieConfiguration());
            builder.ApplyConfiguration(new SeminarieConfiguration());
        }
    }
}

CODE WITH CLASS IMPLEMENTEING IdentityDbContext, THIS CODE DOESN'T WORK AT ALL AND JUST SHOWS WHAT I CAME UP WITH

The applyConfiguration doesn't seem to work. I also don't have any constructors anymore and I don't know wether I still need any.

using Microsoft.AspNet.Identity.EntityFramework;
using ProjectKiwi.Data.Mappers;
using ProjectKiwi.Models.Domain;
using System.Data.Entity;

namespace ProjectKiwi.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public DbSet<Nieuwsartikel> Artikels { get; set; }
        public DbSet<Bestuurslid> Bestuurleden { get; set; }
        public DbSet<Locatie> Locaties { get; set; }
        public DbSet<ContactPagina> ContactPaginas { get; set; }
        public DbSet<Spreker> Sprekers { get; set; }
        public DbSet<Seminarie> Seminaries { get; set; }
        public DbSet<Categorie> Categories { get; set; }
        public DbSet<Deelnemer> Deelnemers { get; set; }
        public DbSet<Inschrijving> Inschrijvingen { get; set; }
        public DbSet<Organisatie> Organisaties { get; set; }
        public DbSet<Forumpost> Forumposts { get; set; }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ApplyConfiguration(new ArtikelConfiguration());
            builder.ApplyConfiguration(new BestuurslidConfiguration());
            builder.ApplyConfiguration(new LocatieConfiguration());
            builder.ApplyConfiguration(new ContactPaginaConfiguration());
            builder.ApplyConfiguration(new CategorieConfiguration());
            builder.ApplyConfiguration(new DeelnemerConfiguration());
            builder.ApplyConfiguration(new InschrijvingConfiguration());
            builder.ApplyConfiguration(new OrganisatieConfiguration());
            builder.ApplyConfiguration(new SeminarieConfiguration());
        }
    }
}

I want to be able to map my classes using my configurations and use this class as my DbContext. I need the DbSets for storing all my data.

Ruben Szekér
  • 1,065
  • 1
  • 10
  • 21
  • For the constructor part, yes add it but i don't think that's the problem, could you tell us what are these errors? – KiKoS Apr 19 '19 at 14:44
  • There are only errors on the lines of ApplyConfiguration and are as following: "DbModelBuilder' does not contain a definition for ApplyConfiguration' and no accessible extension method 'ApplyConfiguration' accepting a first argument of type 'DbModelBuilder' could be found (missing a using directive or an assembly ref?)." However, before changing ModelBuilder to DbModelbuilder it did work but then I get another error in the line of OnModelCreating saying that no suitable method is found to override. And in the line of base.OnModelCreating that it cannot convert ModelBuilder to DBModelbuilder – Ruben Szekér Apr 20 '19 at 07:06

0 Answers0