0

I am developing a login system with ASP.NET Core Identity. I have two identities: Client and Member. When I tried to register a Member user it produces an InvalidOperationException.

ClientUser.cs

public class ClientUser : IdentityUser
{

    [Required]
    [MaxLength(100)]
    public string FirstName { get; set; } = string.Empty;

    [Required]
    [MaxLength(100)]
    public string LastName { get; set; } = string.Empty;
}

MemberUser.cs

public class MemberUser : IdentityUser
    {
        [Required]
        [MaxLength(100)]
        public string MemberFirstName { get; set; } = string.Empty;

        [Required]
        [MaxLength(100)]
        public string MemberLastName { get; set; } = string.Empty;

    }

ClientContext.cs

   public class ClientContext : IdentityDbContext<ClientUser>
{
    public ClientContext(DbContextOptions<ClientContext> options)
        : base(options)
    {
    }


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

    }
}

Program.cs

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("RazorPagesPizzaAuthConnection");

builder.Services.AddDbContext<ClientContext>(options =>
    options.UseSqlServer(connectionString));

//builder.Services.AddDbContext<MemberContext>(options =>
//    options.UseSqlServer(connectionString));
builder.Services.AddDefaultIdentity<ClientUser>(options =>
{
    options.SignIn.RequireConfirmedAccount = true;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireLowercase = false;
    options.Password.RequireUppercase = false;

}).AddEntityFrameworkStores<ClientContext>();


builder.Services.AddIdentityCore<MemberUser>()
    .AddRoles<IdentityRole>()
    .AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<MemberUser, IdentityRole>>()
    .AddEntityFrameworkStores<ClientContext>()
    .AddDefaultTokenProviders()
    .AddDefaultUI();

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddTransient<IEmailSender, EmailSender>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/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.MapRazorPages();

app.Run();

Error:

InvalidOperationException: Cannot create a DbSet for 'MemberUser' because this type is not included in the model for the context.

2
  • 1
    " use single DBContext in multiple identities in ASP.NET Core" I haven't seen this approach so far. If you want two user : Client and Member , maybe you can think about Role-based authorization in ASP.NET Core Commented Sep 19, 2022 at 6:30
  • I believe you can't and this is by design. Commented Mar 13, 2024 at 13:17

1 Answer 1

1

Not sure, but could you want to declare ClientContext as:

public class ClientContext : IdentityDbContext<IdentityUser>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.