0

I'm trying to realize a MVC .NET core application with Identity scaffolded. I stared from an empty MVC app, I added some Identity scaffolded items, with a specific IdentityDbContext and a specific IdentityUser:

public class MyCustomUser : IdentityUser
{
    [PersonalData]
    [Column(TypeName = "nvarchar(100)")]
    public string FirstName { get; set; }
}

Then I created some custom model classes in the Models folder and I would like to force the EF to include those classes in the migration evoked with

Enable-Migrations
Add-Migration "MyMigration"
Update-Database

As a result I expect to see on my DB all the tables for the authentication (AspNetRoles, AspNetUsers (with its new column "FirstName") etc) and the tables created using the classes in \Models.

How can I achieve this? Would you suggest another way to handle identity and models using EF?

Jael
  • 1
  • For clarification, what is the current result you are getting given that you executed the migration commands above? – jegtugado Sep 09 '20 at 09:38
  • The DB includes all and only the tables for the authentication: __EFMigrationsHistory AspNetRoles, AspNetUsers, AspNetRoleClaims, AspNetUserClaims, AspNetUserLogins, AspNetUserRoles, AspNetUserTokens. There is, of course, no table created from the model classes cos' I probably have to expressively instruct the EF to do so. – Jael Sep 09 '20 at 09:43
  • For `IdentityUser`, the table used is `[dbo].[AspNetUsers]` . If you look at the underlying table, the column `FirstName` is present. If you add a new entity in your db context, it should be added to the migration as intended. – jegtugado Sep 09 '20 at 11:59
  • True, can you elaborate on the correct way to handle this kind of situation i.e: Classes\Entities for authentication and authorization and Classes\Entities related to the application domain. Does the two goes together using one DBContext or not or other considerations. – Jael Sep 09 '20 at 14:06
  • Hi, @Jael, please check [How to extend IdentityUser with custom property ](https://stackoverflow.com/a/40579369/13655939). – Michael Wang Sep 09 '20 at 14:21
  • Sorry, but I already extended IdentityUser. I raised another question – Jael Sep 09 '20 at 16:19
  • I believe this is just how Identity works. You cannot have a custom 'user' table since it is managed by the `IdentityDbContext` which is a cherry on top of `DbContext`. Every other entity is handled by the original `DbContext`. Note that you can have a separate context for your identity entities and other entities. You are not forced to place them all in a single context. – jegtugado Sep 11 '20 at 11:24

0 Answers0