EF Core 3.1 Identity model customization - What did I do wrong?

.net-core asp.net-core-identity c# entity-framework-core

Question

Not sure what I am doing wrong and why it is creating double navigation for each table. I am reading the guidance from https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-3.1.

This has been driving me nuts and I need to resolve this before going live next week.

SQL Diagram

Any help would be much appreciated.

Entities:



public class AppUser : IdentityUser<long>
    {
        public bool HasResetPassword { get; set; }
        public DateTime? LastLogIn { get; set; }
        public DateTime? DateRegistered { get; set; }
        public DateTime? RegistrationDate { get; set; }

        public override string ToString()
        {
            return UserName;
        }
        public virtual ICollection<AppUserClaim> Claims { get; set; }
        public virtual ICollection<AppUserLogin> Logins { get; set; }
        public virtual ICollection<AppUserToken> Tokens { get; set; }
        public virtual ICollection<AppUserRole> UserRoles { get; set; }


    }
    public class AppRole : IdentityRole<long>
    {
        public string Description { get; set; }
        public virtual ICollection<AppUserRole> UserRoles { get; set; }

        public override string ToString()
        {
            return $"{Name} - {Description}";
        }
    }
    public class AppUserRole : IdentityUserRole<long>
    {
        public virtual AppUser User { get; set; }
        public virtual AppRole Role { get; set; }
    }
    public class AppUserClaim : IdentityUserClaim<long>
    {
        public virtual AppUser User { get; set; }
    }

    public class AppUserLogin : IdentityUserLogin<long>
    {
        public virtual AppUser User { get; set; }
    }

    public class AppRoleClaim : IdentityRoleClaim<long>
    {
        public virtual AppRole Role { get; set; }
    }

    public class AppUserToken : IdentityUserToken<long>
    {
        public virtual AppUser User { get; set; }
    }
DbContext public partial class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, long, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } ... protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) { relationship.DeleteBehavior = DeleteBehavior.Restrict; } #region Identity modelBuilder.Entity<AppUser>(b => { // Each User can have many UserClaims b.HasMany(e => e.Claims) .WithOne() .HasForeignKey(uc => uc.UserId) .IsRequired(); b.HasKey(u => u.Id); b.Property(p => p.Id).ValueGeneratedOnAdd(); // Each User can have many UserLogins b.HasMany(e => e.Logins) .WithOne() .HasForeignKey(ul => ul.UserId) .IsRequired(); // Each User can have many UserTokens b.HasMany(e => e.Tokens) .WithOne() .HasForeignKey(ut => ut.UserId) .IsRequired(); // Each User can have many entries in the UserRole join table b.HasMany(e => e.UserRoles) .WithOne() .HasForeignKey(ur => ur.UserId) .IsRequired(); }); modelBuilder.Entity<AppRole>(b => { b.HasKey(u => u.Id); b.Property(p => p.Id).ValueGeneratedOnAdd(); // Each Role can have many entries in the UserRole join table b.HasMany(e => e.UserRoles) .WithOne(e => e.Role) .HasForeignKey(ur => ur.RoleId) .IsRequired(); }); modelBuilder.Entity<AppUserRole>(b => { b.HasKey(u => new { u.RoleId, u.UserId }); }); modelBuilder.Entity<AppUserClaim>(b => { b.HasKey(u => u.Id); b.Property(p => p.Id).ValueGeneratedOnAdd(); }); modelBuilder.Entity<AppUserLogin>(b => { b.HasKey(u => new { u.LoginProvider, u.ProviderKey }); }); modelBuilder.Entity<AppRoleClaim>(b => { b.HasKey(u => u.Id); b.Property(p => p.Id).ValueGeneratedOnAdd(); }); modelBuilder.Entity<AppUserToken>(b => { b.HasKey(u => new { u.UserId, u.LoginProvider }); }); #endregion
1
1
3/20/2020 6:55:49 AM

Popular Answer

I believe you are doing useless work, what would you like to achieve? what is the double foreign key for?

if you just want to add columns to security tables, follow this post it is written in a simple and precise way:

How to add Custom User Properties in Identity Membership System

this is a small example:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace Identity.Models
{
    public class AppUser : IdentityUser
    {
        public Country Country { get; set; }

        public int Age { get; set; }

        [Required]
        public string Salary { get; set; }
    }

    public enum Country
    {
        USA, UK, France, Germany, Russia
    }
}
0
3/31/2020 2:29:37 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow