最近,我想在我的C#MVC Web应用程序中实现身份验证,因此我开始遵循Claim based authorization design for conditional edit operation in ASP.NET MVC App中的指南。但是,当涉及到我的DbContext类时,我遇到了一个问题。
如果没有出现多个错误,我似乎无法使其正常工作。如果我解决其中一个问题,另一个将中断。我尝试查找它,但似乎没有人遇到和我一样的问题。
我不明白为什么它会中断,因为从现在开始继承的类仍然从DbContext继承,它们之间只有2个类。
使用类实现DbContext的代码,此代码的工作需要更改
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());
}
}
}
带有实现IdentityDbContext的类的代码,此代码根本不起作用,只是显示了我要解决的问题
applyConfiguration似乎不起作用。我也不再有任何构造函数,而且我不知道我是否还需要任何构造函数。
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());
}
}
}
我希望能够使用我的配置来映射我的类并将该类用作我的DbContext。我需要DbSet来存储所有数据。