0

EDIT: I should probably add that in my project (which was based on the ASP.NET Core 2.1 - WebApp with MVC, with "Individual User Accounts), I don't have "AppUser" (or an equivalent class that inherits from IdentityUser), and ApplicationDbContext inherits from IdentityDbContext - not from IdentityDbContext (or IdentityDbContext).

I'm currently learning / starting with ASP.NET Core and use "Add New Scaffolding Item" to Add Custom user data to Identity and noticed that I cannot use the existing context (ApplicationDbContext) if I want the scaffolder to create my custom user class. The only way to have that field enabled is if I add a new context, which will also add a new connection string. But the new context seems to be essentially doing the same that my already existing context does.

I also noticed that the two new classes (AppExtendedContext and AppExtendedUser in my case) were put under Areas/Identity/Data, while I'd actually expect them to be under Data and Models.

So I was wondering: Why? ;-)

Or, to put it in more actionable terms: Would it be safe to refactor the generated code back to only use a single ApplicationDbContext? What would I be losing / what kind of trouble would I get myself into?

The answer could be as simple as "for pragmatic reasons" (e.g. because it makes the automatic code generation easier / safer); and in that case, moving these things around should be fairly safe as long as I'm not going to use the scaffolder again. But then again, it might make life more difficult/confusing when upgrading (e.g. to ASP.NET Core 2.2, or ASP.NET Core 3.0).

Jashan
  • 180
  • 1
  • 9
  • What do you mean with you " cannot use the existing context"? `ApplicationDbContext`is where you are supposed to do your CUSTOMIZATIONS in. It inherits from `IdentiytyDbContext` where `TUser` is your user class (typically `AppUser` in the templates) and when you do customizations you just make your adjustments to `AppUser` – Tseng Jul 23 '18 at 09:39
  • Also you would lose nothing, because `AppUser` is already a scafolded class. The Identity class is named `IdentityUser` from which `AppUser` inherits – Tseng Jul 23 '18 at 09:41
  • Maybe that's a change for ASP.NET Core from 2.0 to 2.1 - but as I said in my comment to your answer: I don't have "AppUser", so I can't do customizations without adding a class like that. My ApplicationDbContext does not inherit from IdentityDbContext but just IdentityDbContext. – Jashan Jul 23 '18 at 10:22

2 Answers2

1

Just trash your AppExtendedContext and AppExtendedUser classes and use the ApplicationDbContext and AppUser and add your customizations there.

They are ready for modification (they inherit from IdentityDbContext<TUser> and IdentityUser<TKey> respectively. Feel free to rename them to something more familiar to your application (such as Customer or CustomerUser), if you don't like the AppUser name.

There's really no need to inherit from AppUser or ApplicationDbContext, they are the final/concrete classes for your Core Identity.

Tseng
  • 52,202
  • 10
  • 166
  • 183
  • Cool, thank you. I don't have an AppUser, though, which was why I did the scaffolding: "ApplicationDbContext : IdentityDbContext" but "AppExtendedContext : IdentityDbContext". Also, in Startup, I have: services.AddDefaultIdentity().AddEntityFrameworkStores(); ... which I believe I probably still need to change, too. – Jashan Jul 23 '18 at 10:19
  • Could be that the newer templates don't have the `AppUser` anymore. It used to be there in the past. – Tseng Jul 24 '18 at 08:04
0

Make sure your context inherits from IdentityDbContext<>. You have to specify the type to use for your application user, which you can just create as a class that extends IdentityUser (given the fact you want to start extending the IdentityUser class). Go for something like YourCustomContextType : IdentityDbContext<YourCustomUserType>.

As for the Identity services registration you need to specify your class there too using AddDefaultIdentity<YourCustomUserType>().AddEntityFrameworkStores<YourCustomContextType>().

Make sure you also look at how other people approach extending the Identity functionality:

Ropstah
  • 16,688
  • 23
  • 112
  • 183