'Using existing .Net Identity database in .NET CORE application

im new to identity core and am trying to create a mvc app using .Net Core 3.1.1 and hit a roadblock on identity.

My toughest restriction is that I need to use the old Identity database from an old MVC5 app. The app is in use and this means the table structure cannot be changed.

Followed the steps described in Sql Exeption: Invalid column name "NormalizedUserName..." in existing .net framework identiy db used by .net core project

Got Usermanager and models set up in startup:

public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext<ApplicationDbContext>(options =>
              options.UseSqlServer(
                  Configuration.GetConnectionString("RaksaConnection")));
         
          services.AddDbContext<RaksaDbContext>(options=>
          options.UseSqlServer(
              Configuration.GetConnectionString("RaksaConnection")));

          services.AddIdentity<ApplicationUser, ApplicationRole>()
      .AddEntityFrameworkStores<ApplicationDbContext>()
      .AddDefaultTokenProviders();
          //services.AddTransient <UserManager<ApplicationUser>>();
          services.AddControllersWithViews();
          services.AddRazorPages();
      }

And models copied over from older site:

public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {
            this.Id = Guid.NewGuid().ToString();
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        private string PF_FName;
        private string PF_FNameAndCompany;
        public int? NeptonUserId { get; set; }
        public string NeptonUserName { get; set; }
        public int? NeptonUserPersonnelNumber { get; set; }
        public virtual Guid CompanyDataId { get; set; }
        public override string UserName { get; set; }
        public override string NormalizedEmail { get { return this.Email.ToUpperInvariant(); } }
        public override string NormalizedUserName { get { return this.UserName.ToUpperInvariant(); } }
        public DateTime? LockoutEndDateUtc { get; set; }
        private string PF_CompanyVat { get; set; }
        private string PF_JobTitleValue { get; set; }
        private DateTime PF_AccountCreated { get; set; }
        public bool GIG_worker { get; set; }
        public DateTime? LastLogin { get; set; }
        public int CommLevelId { get; set; }
        [Display(Name = "Nimi")]
        public string FullName
        {
            get
            {
                this.PF_FName = FirstName + " " + LastName;
                return this.PF_FName;
            }
        }
        
        public DateTime AccountCreated
        {
            get
            {
                return this.PF_AccountCreated;
            }
            set
            {
                this.PF_AccountCreated = value;
            }
        }
        private bool PF_Isrejected { get; set; }
        public bool IsRejected
        {
            get
            {
                return this.PF_Isrejected;
            }
            set
            {
                this.PF_Isrejected = value;
            }
        }
        public bool Active { get; set; }
        public string JobTitle
        {
            get
            {
                return this.PF_JobTitleValue;
            }
            set
            {
                this.PF_JobTitleValue = value;
            }
        }
    }
public class ApplicationRole : IdentityRole
{
        public ApplicationRole()
        {
            base.Id = Guid.NewGuid().ToString();
        }
        public ApplicationRole(string name) : this()
        {
            base.Name = name;
        }
        public ICollection<Permission> Permissions { get; set; }
    }
    
public class Permission
    {
        public Permission()
        {
            this.PermissionId = Guid.NewGuid().ToString();
            this.RolesWithPermission = new HashSet<ApplicationRole>();
        }

        public string PermissionId { get; set; }
        public string PermissionDescription { get; set; }

        public ICollection<ApplicationRole> RolesWithPermission;
       
    }
}

Can get the usermanager to list the users just fine, but SignInManager compalains about NormalizedUsername field:


    InvalidOperationException: The LINQ expression 'DbSet<ApplicationUser>
.Where(a => a.NormalizedUserName == __normalizedUserName_0)' could not be translated.
 Either rewrite the query in a form that can be translated, or switch to client evaluation 
explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), 
or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Don't know where to go next, do I have to customize signInmanager or is there a more simple solution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source