No1Lives4Ever
No1Lives4Ever

Reputation: 6883

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.

enter image description here

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

Upvotes: 1

Views: 4403

Answers (4)

user3815522
user3815522

Reputation: 21

Try this

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

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

Upvotes: 0

Darjan Bogdan
Darjan Bogdan

Reputation: 3900

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 e.g. 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.

Upvotes: 1

Wijitha
Wijitha

Reputation: 1389

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,

Upvotes: 0

Iemanuel
Iemanuel

Reputation: 51

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

Upvotes: 0

Related Questions