简体   繁体   中英

AspNetUsers to IdentityUser with Entity Framework Core

Background

I have a project written with C# and ASP.NET core (v2.2). This project is connected to a database that holds a table called "AspNetUsers". The building strategy is "Database First".

I'm building the models from the data with this command:

Scaffold-DbContext "...DB-CS..." PROVIDER -Context UsersDB -f -OutputDir Processes\Models

Then it's created for me this mapped class:

public partial class AspNetUsers
{
    public AspNetUsers()
    {

    }
    public string Id { get; set; }
    public DateTime RegistrationTime { get; set; }
    // ...
}

On my website, I'm using the UserManager class. This class is expecting to get an Object that inherits from the IdentityUser interface.

The Question:

Is this possible to make my AspNetUsers class implementing IdentityUser ?

Thanks!

Update #1

Thanks for @Darjan Bogdan answer, I created this new file:

public partial class AspNetUsers : IdentityUser
{
}

and changed my UserManager to looks like this:

private UserManager<AspNetUsers> UserManager { get; set; }

On first looks, it looks like work. When going deeper to check this - I got many strange errors like :

User security stamp cannot be null.

Although AspNetUsers instance has SecuriyStamp field (not null). I checked the AspNetUsers file (auto-generated) and it seems like some properties are hiding another.

在此处输入图片说明

Question: I working in Database first model. So, the AspNetUsers file is auto-generated. How to avoid hiding other properties?

You can easily do it, there is a reason why code generators create partial classes .

So, in order for AspNetUsers to inherit IdentityUser you just need to define following in another (not generated) file, in eg AspnetUsers.Partial.cs file:

public partial class AspnetUsers : IdentityUser
{

}

Please note, that you should not change the file which is generated, but rather create your own folder in which you will add additional files with partial classes.

Worth to mention, you need to use the same namespace when extending partial class.

You dont realy need to implement IdentityUser unless you have custom data on it

using System;

namespace WebApp1.Areas.Identity.Data
{
    public class WebApp1User : IdentityUser
    {
        [PersonalData]
        public string Name { get; set; }
        [PersonalData]
        public DateTime DOB { get; set; }
    }
}

Take a look here Microsoft docs

Yeah its possible and that's the normal way you should do it. In you case, from the auto generated AspNetUser class, remove properties that are inhering from the IdentityUser and keep only that are not inhering.

public class AspNetUser : IdentityUser<string> 
{
    public DateTime RegistrationTime { get; set; }
    public string OtherPros {get;set;},
}

Then you can use AspNetUser class with UserManager.

Cheers,

Try this

public partial class AspNetUsers : IdentityUser
{
    public AspNetUsers()
    {

    }
    public string Id { get; set; }
    public DateTime RegistrationTime { get; set; }    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM