C# 中的身份 ASP.NET 核心 Web API 用户

Identity in C# ASP.NET Core Web API user

我有一个关于房地产的项目,所以我创建了 tables user 并 link 用 estates 编辑了它,并且我了解了身份。

当我迁移时它隐藏了 user table 因为 ASP.NET 核心标识已经有一个 users table,所以我怎么能 link asp.net 用户到用户或如何 link asp.net 用户到身份用户

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace Try.DAL.Entity
{
        [Table("Users")]
    public class Users
    {
        [Key]
        public int Id { get; set; }

        [StringLength(50)]
        public string Fname { get; set; }
        [StringLength(50)]
        public string Lname { get; set; }

        public string Email { get; set; }
        public string Password { get; set; }
        [StringLength(20)]
        public string Phone { get; set; }
        public DateTime Signupdate { get; set; }

        public int Usergroupid { get; set; }
        [ForeignKey("Usergroupid")]
        public UserGroup Usergroup { get; set; }
        public virtual ICollection<Estate> Estate { get; set; }
   

    }
}




 
using Microsoft.EntityFrameworkCore;
using Try.DAL.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Try.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
//using Try.Models;

namespace Try.DAL.Database
{
    public class DbContainer : IdentityDbContext
    {
        public DbContainer(DbContextOptions<DbContainer> opts) : base(opts) { }
        public virtual DbSet<RefreshToken> RefreshTokens { get; set; }
        public DbSet<Users> Users { get; set; }
        public DbSet<Ads> Ads { get; set; }
        public DbSet<Clients> Clients { get; set; }
        public DbSet<Feedback> Feedback { get; set; }
        public DbSet<Interests> Interests { get; set; }
        public DbSet<Orders> Orders { get; set; }
        public DbSet<Estate> Estate { get; set; }
        public DbSet<users> users{ get; set; }



   

您必须像这样在 applicationDbContext.cs 文件中添加要添加到身份用户 table 的字段:

namespace Try.DAL.Database
{
    // You add this class here with custom fields.
    public class ApplicationUser : IdentityUser
    {
        // add properties here.
        [StringLength(50)]
        public string Fname { get; set; }
        [StringLength(50)]
        public string Lname { get; set; }

        public string Email { get; set; }
        public string Password { get; set; }
        [StringLength(20)]
        public string Phone { get; set; }
        public DateTime Signupdate { get; set; }

        public int Usergroupid { get; set; }
        [ForeignKey("Usergroupid")]
        public UserGroup Usergroup { get; set; }
        public virtual ICollection<Estate> Estate { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        public DbSet<Users> Users { get; set; }
        public DbSet<Ads> Ads { get; set; }
        public DbSet<Clients> Clients { get; set; }
        public DbSet<Feedback> Feedback { get; set; }
        public DbSet<Interests> Interests { get; set; }
        public DbSet<Orders> Orders { get; set; }
        public DbSet<Estate> Estate { get; set; }
    }
}

修改您的 Dbcontext class 如下:

public class UserTestDbContext : IdentityDbContext

{
    public UserTestDbContext(DbContextOptions<UserTestDbContext> options)
        : base(options)
    {
    }

    public DbSet<_3._7.Models.User> User { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>().ToTable("User");
    }
}

我将 class 简化如下:

public class User:IdentityUser

{
    public int Id { get; set; }
    public string Name { get; set; }
   
    public string Password { get; set; }
  }

结果: 迁移 class: 和数据库:

如果您修改 dbcontext class 如下:

public class UserTestDbContext : IdentityDbContext<User>
{
    public UserTestDbContext(DbContextOptions<UserTestDbContext> options)
        : base(options)
    {
    }

    public DbSet<_3._7.Models.User> User { get; set; }    
}

您会发现用户 class 的属性已添加到 ASPNetUser Table。