[How-To] Add support for ASP.NET Identity Core + Entity Framework Core

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

[How-To] Add support for ASP.NET Identity Core + Entity Framework Core

Post by RobertK » Sun 09 Apr 2017 23:47

Since DevArt "Entity Developer" does not support ASP.NET Identity, I decided to write the following guide.

This guide will show you how to add support for ASP.NET Core Identity using DevArt Entity Developer (Entity Framework core).

Set up Identity for use with Entity Developer
1) Create an Identity ASP.NET Core web app in Visual Studio; add support for Postgres (optional); and identity core support; run "Update-Database" via Package Manager Console (this creates all identity tables)

ASP.NET Core MVC Identity using PostgreSQL database: https://medium.com/@RobertKhou/asp-net- ... 52255f67c4
Customise ASP.NET Core MVC Identity: https://medium.com/@RobertKhou/changing ... 103ddde0c3

2) Open DevArt Entity Developer, select EFCore template and Import all tables (i.e Models -> Update Models from database)

Entity Framework Core model template
1) Set "Model Name as Files Prefix" to False
2) Set Validation Framework to "Data Annotations" (Optional, Entity Developer generate Navigation properties)

ApplicationDbContext (i.e ApplicationDbContext is the name of the model)
1) Add support for ASP.NET Core Identity
a) Add identity inheritances
public partial class ApplicationDbContext : IdentityDbContext<User, <IdentityRole<long>, long, IdentityUserClaim<long>, IdentityUserRole<long>, IdentityUserLogin<long>, IdentityRoleClaim<long>, IdentityUserToken<long>>


2) Inside "OnModelCreating" method
a) Add Snake casing translator (Optional: Postgresql, MySQL we need this!)
Remember to add the _FixSnakeCaseNames method at the bottom of "ApplicationDbContext.cs"

Code: Select all

 #region FixSnakeCaseNames
        private void _FixSnakeCaseNames(ModelBuilder modelBuilder)
        {
            var mapper = new Npgsql.NpgsqlSnakeCaseNameTranslator();

            foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                // modify column names
                foreach (var property in entity.GetProperties())
                {
                    property.Relational().ColumnName = mapper.TranslateMemberName(property.Relational().ColumnName);
                }

                // modify table name
                entity.Relational().TableName = mapper.TranslateMemberName(entity.Relational().TableName);

                // move asp_net tables into schema 'identity'
                if (entity.Relational().TableName.StartsWith("asp_net_"))
                {
                    entity.Relational().TableName = entity.Relational().TableName.Replace("asp_net_", string.Empty);
                    //entity.Relational().Schema = "identity";
                }
            }
        }
        #endregion
b) Add the following code to rename entity (Optional)

Code: Select all

            modelBuilder.Entity<User>().ToTable("AspNetUser");
            modelBuilder.Entity<IdentityRole<long>>().ToTable("AspNetRole");
            modelBuilder.Entity<IdentityUserClaim<long>>().ToTable("AspNetUserClaim");
            modelBuilder.Entity<IdentityUserLogin<long>>().ToTable("AspNetUserLogin");
            modelBuilder.Entity<IdentityUserRole<long>>().ToTable("AspNetUserRole");
            modelBuilder.Entity<IdentityUserToken<long>>().ToTable("AspNetUserToken");
            modelBuilder.Entity<IdentityRoleClaim<long>>().ToTable("AspNetRoleClaim");
3) Remove Identity entities from "DbSets"
User
Role
UserClaim
UserRole
UserLogin
RoleClaim
UserToken

4) Remove Identity property Mapping, keep only custom properties.
5) Remove Identity Relationship Mapping (Navigation properties), keep only custom relationships.

Class Models
1) Add support for ASP.NET Core Identity
a) To header
"using Microsoft.AspNetCore.Identity.EntityFrameworkCore;"
User.cs
Role.cs
UserClaim.cs
UserRole.cs
UserLogin.cs
RoleClaim.cs
UserToken.cs

b) Add Identity inheritance
User.cs - public partial class User : IdentityUser<long>
Role.cs - public partial class UserClaim : IdentityUserClaim<long>
UserClaim.cs - public partial class UserClaim : IdentityUserClaim<long>
UserRole.cs - public partial class UserRole : IdentityUserRole<long>
UserLogin.cs - public partial class UserLogin : IdentityUserLogin<long>
RoleClaim.cs - public partial class RoleClaim : IdentityRoleClaim<long>
UserToken.cs - public partial class UserToken : IdentityUserToken<long>

2) Remove all Identity properties
User.cs
Role.cs
UserClaim.cs
UserRole.cs
UserLogin.cs
RoleClaim.cs
UserToken.cs
Last edited by RobertK on Mon 10 Apr 2017 05:56, edited 9 times in total.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: [How-To] Add support for ASP.NET Identity Core

Post by RobertK » Sun 09 Apr 2017 23:55

If you know a better way to add support for ASP.NET Identity Core via Devart Entity Developer, let me know! :)

Post Reply