Sin Lee
Sin Lee

Reputation: 135

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

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; }
}

Upvotes: 7

Views: 12629

Answers (6)

Zabulon Mobwa
Zabulon Mobwa

Reputation: 11

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.

Upvotes: 1

manjunathmayur
manjunathmayur

Reputation: 1

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);

Upvotes: 0

rajnish kumar
rajnish kumar

Reputation: 80

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.

Upvotes: 1

Aamer
Aamer

Reputation: 81

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

Upvotes: 0

DHelm
DHelm

Reputation: 11

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.

Upvotes: 1

Bryan van Rijn
Bryan van Rijn

Reputation: 971

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

Upvotes: 8

Related Questions