将我自己的构建与 ASP.NET 身份相关联时出现问题

Problem associating my own builds with ASP.NET Identity

我一直在为我的实习开发一个项目,我可能设置了错误的结构,因为在项目开发过程中不断要求额外的更新。我的项目中有 2 个独立的上下文;一个用于身份,另一个用于我自己的实体。我的问题是:我想根据 EntityLayer 中的 CustomUser 创建 Identity tables(这里没问题),然后我想将 Identity(即用户)与我自己的实体相关联。虽然我没有在 dataaccess 的上下文中定义 customusers,它会创建 CustomUser table 并与其建立关系,但我想要的是用 Identity.

设置它

在这里,与 EmployeeDemands、EmployeeServices 或其他 table 的关系应该直接与 AspNetUsers 建立关系,而不是 CustomUser,我无法解决这个问题。

身份的 UI 层上下文中的代码。

   using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.AspNetCore.Identity;

namespace CompanyPanelUI.Data
{
   public class ApplicationDbContext : IdentityDbContext<CustomUser>
   {
       public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
           : base(options)
       {
       }
       
      
   }  
   }
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace DataAccessLayer.Concrete
{
    public class Context:DbContext
    {

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("server=(localdb)\MSSQLLocalDB;database=CompanyPanelNew_DB; integrated security=true;");
        }

        public DbSet<Demand> Demands {get; set;}
        public DbSet<Firm> Firms {get; set;}
        public DbSet<Service> Services {get; set;}
        public DbSet<FirmService> FirmServices {get; set;}
        public DbSet<User> Users { get; set;}
        public DbSet<DemandAnswer> DemandAnswers { get; set;}
        public DbSet<DemandFile> DemandFiles { get; set;}
        public DbSet<Department> Departments { get; set; }
        public DbSet<DepartmentEmployee> DepartmentEmployees { get; set; }
        public DbSet<EmployeeService> EmployeeServices { get; set; }
        public DbSet<EmployeeDemand> EmployeeDemands { get; set; }

    }
}

EntityLayer 自定义用户

using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;

namespace EntityLayer.Concrete
{
    public class CustomUser:IdentityUser
    {
        [Display(Name = "Ad Soyad")]
        public string NameSurname { get; set; }

        [Display(Name = "Başvurulan Şirket")]
        public string ApplicationFirm { get; set; }

        [Display(Name = "Firma Id")]
        public int? FirmId { get; set; }
        
        [Display(Name = "Bolum Id")]
        public int? DepartmentId { get; set; }
        

    }
}

例如,我试图在 EmployeeServices table 中建立的关系。

using System.ComponentModel.DataAnnotations;

namespace EntityLayer.Concrete
{
    public class EmployeeService
    {
        [Key]
        public int EmployeeServicesId { get; set; }
        public int ServiceId { get; set; }
        public Service Services { get; set; }
        public string Id { get; set; }
        public CustomUser User { get; set; }
    }
}

您应该在 ApplicationDbContext 中配置 Users table,如下所示:

public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder); 
            builder.Entity<CustomUser>().ToTable("CustomUser"); 
           // This will configure the table name of the Identity users table. 
           // If you wish you can rename the other tables too.           
        }
}

您可能想要重新创建数据库。 另一方面,您可能还想忽略为 Context DbContext 中的 CustomUser 实体生成的迁移。为此,您应该在 Context DbContext:

中配置 CustomUser 实体
builder.Entity<CustomUser>().ToTable("CustomUser", build => build.ExcludeFromMigrations());