Reputation: 6883
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.
Is this possible to make my AspNetUsers
class implementing IdentityUser
?
Thanks!
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?
Upvotes: 1
Views: 4403
Reputation: 21
Try this
public partial class AspNetUsers : IdentityUser
{
public AspNetUsers()
{
}
public string Id { get; set; }
public DateTime RegistrationTime { get; set; }
}
Upvotes: 0
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
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
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