在.NET Core应用程序中使用现有的.NET身份数据库

发布于 2025-01-30 06:18:32 字数 4778 浏览 3 评论 0 原文

我是身份核心的新手,并且正在尝试使用.NET Core 3.1.1创建MVC应用程序,并遇到身份障碍。

我最艰难的限制是我需要使用旧的MVC5应用程序中的旧身份数据库。该应用程序正在使用,这意味着无法更改表结构

遵循

在启动中设置了Usermanager和模型:

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();
      }

从较旧站点复制的模型:

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;
       
    }
}

可以让Usermanager列出用户,列出用户的良好,但是SignManager compalains围绕归一化使用字段:


    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.

不知道接下来要去哪里,我必须自定义SigninManager还是有一个更简单的解决方案?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文