7

I'm creating a Profile for a user, adding columns to ApplicationUser and then executing the DbSet command at ApplicationDbContext. Just writing the DbSet command, it will throw an error:

InvalidOperationException: Unable to materialize entity instance of type 'IdentityUser'. No discriminators matched the discriminator value ''.

What did I do wrong and where? How can I fix it? Just dropping DbSet at ApplicationDbContex, everything can run normally.

Enter image description here

Code of the ApplicationUser:

public class ApplicationUser : IdentityUser
{
    [Display(Name = "Full Name")] public string FullName { get; set; }
    public DateTime CreateAt { get; set; }
    public DateTime? UpdateAt { get; set; }
    public string ImagePath { get; set; }

    public ApplicationUser()
    {
        CreateAt = DateTime.Now;
        UpdateAt = DateTime.Now;
    }
}

The ApplicationDbConText:

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

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
}
1
  • Maybe you should see this doc. Commented Jul 1, 2021 at 9:21

6 Answers 6

8

It seems that you're missing the ApplicationUser as a generic argument for your context.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

See more in detail: Customize identity model

Sign up to request clarification or add additional context in comments.

8 Comments

I tried but it didn't seem to work so I came here to ask everyone.
@SinLee You have to specify it anyway. Did you mention ApplicationUser class anywhere else? For example in services.AddIdentity<ApplicationUser>?
@feihoa I use services.AddIdentity<IdentityUser, IdentityRole>() .AddDefaultUI() .AddDefaultTokenProviders() .AddEntityFrameworkStores<ApplicationDbContext>();
@SinLee Try services.AddIdentity<ApplicationUser, IdentityRole> instead. And be sure that your _userManager instance is an instance of UserManager<ApplicationUser> class.
@feihoa I just deleted the data in the database and it succeeded without changing anything ...
|
1

I don't know if this will help at all. I just tried creating an extension table for IdentityUsers after having created a test user in the database; ran into this error, and when I checked the database it added a Discriminator attribute to the IdentityUser table. So for my test user I put "IdentityUser" in as the discriminator and it loaded the page up perfectly fine. It might be because that user was logged in and it couldn't load the default unlogged in page, but figured I would throw that out there.

My assumption is that it was trying to load the user, but it didn't know how to handle a blank discriminator spot and it doesn't assign a default discriminator value to previously created users.

1 Comment

This simple solution worked for me.
1

I face this exception too. To fix it, simply I go to my database open the table which contains “Discriminator” column and delete the records and run the application. Rest things are handled by Entity framework .I hope this will work for you as well.

Comments

1

The discussion is really old, but I had the same error, I found that discussion, it put me on the way, but required more research to find the solution to my problem, I'll just explain the way I solved my problem.

When you add your own user class to your project, the user class is often rename ApplicationUser, a class that's inheriting the IdentityUser, before making any migration, you have to go in the ApplicationDbContext file, and add it as a generic argument for the context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

after that you have always to make some changes in the _LoginPartial.cshtml file (Views > Shared > _LoginPartial.cshtml) in changing these lines :

@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

by those

@using Microsoft.AspNetCore.Identity
@using YourProjectName.Data
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

Don't forget to replace YourProjectName by the name of your project

After that you must go in the Program.cs File and replace the IdentityUser at line 13 :

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

remplace the reference to the IdentityUser by the ApplicationUser reference, it should look like that :

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();

And if you have a Startup.cs file in your project, you should make some changes a well, at line 41 you'll find there the following line :

services.AddDefaultIdentity<IdentityUser>()
   .AddDefaultUI(UIFramework.Bootstrap4)
 .AddEntityFrameworkStores<ApplicationDbContext>();

replace the instruction by the following :

services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
   options.User.RequireUniqueEmail = false;
})
   .AddDefaultUI(UIFramework.Bootstrap4)
 .AddEntityFrameworkStores<ApplicationDbContext>()
   .AddDefaultTokenProviders();

don't forget to remove spaces between some instructions I pasted, here.

more details in that course, and in the ASP.NET Core Documentation

What I write was Already written, here or other discussion, I just tried to put together all those answers and tried to add some precisions, the link gave by @feihoa really helped me. I hope my answer will help anyone.

Regards.

Comments

0

In your Startup.cs or Program.cs (it depends whether you use ASP.NET Core 6.x or earlier).

Change this:

builder.Services.AddDefaultIdentity<IdentityUser>

To this:

builder.Services.AddDefaultIdentity<ApplicationUser>

Do not forget. In addition to modify that in your Shared/_LoginPartial.cshtml:

Those:

@inject SignInManager<IdentityUser> SignInManager

@inject UserManager<IdentityUser> UserManager

To those:

@inject SignInManager<ApplicationUser> SignInManager

@inject UserManager<ApplicationUser> UserManager

Comments

0

When registering check if email is unique, Example

var emailAlreadyUsed = _userManager.Users.Any(x => x.Email == user.Email);
if (emailAlreadyUsed){
    ModelState.AddModelError(string.Empty, "Email already used");
    return Page();
}
var result = await _userManager.CreateAsync(user, Input.Password);

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.