Del
Del

Reputation: 416

asp.net 5 / Core Identity Customisation Issues - No Implicit Reference Conversion error

So I've been looking at this for several hours now and i'm starting to lose the little hair I have left on it...

I'm testing this on an entirely new website. No worries about dropping tables etc.

I'm using Asp.net 5 / Core with the Identity tools to handle user registration, roles etc.

I want to customise the IdentityUser class to include a few custom fields, like displayname, and description, plus some references to other tables. (E.g. Users will create a Galleries of images, so the gallery table will have a foreign key to the UserId.)

However, following various tutorials online, (https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0) I am advised to do the following:

Create a new class for a user with my custom fields, and inherit from the IdentityUser, e.g. UserAccount : IdentityUser.

public class UserAccount : IdentityUser
{
    [Required, MaxLength(50)]
    public string DisplayName { get; set; }

    [MaxLength(512)]
    public string Description { get; set; }

Update the ApplicationDbContext, to inherit from IdentityDbContext<NewUserClass>

enter image description here

I just keep getting the following error: Error CS0311 The type '...UserData.UserAccount' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext'. There is no implicit reference conversion from '....UserData.UserAccount' to 'Microsoft.AspNetCore.Identity.IdentityUser'.

What am I missing here?? None of the tutorials seem to have this casting issue.

Upvotes: 0

Views: 511

Answers (1)

Del
Del

Reputation: 416

Ok, so now I feel like an idiot! Posting this here incase anyone else has a similar experience!!!

In your custom class to extend the IdentityUser class, my Visual Studio defaulted to add the reference

using Microsoft.AspNet.Identity.EntityFramework;

This is the wrong one! You need to use the following one

using Microsoft.AspNetCore.Identity;

It's obvious now, but I've honestly just spent like 3h trying to get to the bottom of this....

The confusion came in from the NuGet packages you have installed.

I needed the following

Microsoft.AspNetCore.Identity.EntityFramework

If you have

Microsoft.AspNetCore.Identity or
Microsoft.Extensions.Identity.Core or
Microsoft.AspNet.Identity.Core or

any of the other similar ones, you will have issues!!

I hope that helps someone!

Upvotes: 0

Related Questions