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

发布于 2025-01-12 05:38:33 字数 2083 浏览 3 评论 0原文

我有一个关于房地产的项目,所以我创建了表 user 并将其与 estates 链接起来,然后我了解了身份。

当我迁移时,它会隐藏 user 表,因为 ASP.NET Core 身份已经有一个 users 表,所以我如何将 asp.net 用户链接到用户或如何链接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; }



   

I have a project it's about real estate so I created tables user and linked it with estates, and I learnt about identity.

When I migrate it hides the user table because the ASP.NET Core identity already has a users table, so how can I link asp.net user to users or how to link asp.net user to identity user

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



   

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2025-01-19 05:38:33

修改您的 Dbcontext 类如下:

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

我已简化该类如下:

public class User:IdentityUser

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

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

“DB1”
DB2

如果您按如下方式修改 dbcontext 类:

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

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

您可以发现 user 类的属性已添加到 ASPNetUser 表中。
输入图片此处描述

Modify your Dbcontext class as below:

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

And I have simpfied the class as below:

public class User:IdentityUser

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

Result:
The migration class:
migration
and the database:

DB1
DB2

And if you modify the dbcontext class as follow:

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

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

You could find the properties of user class has been added to the ASPNetUser Table.
enter image description here

我早已燃尽 2025-01-19 05:38:33

您必须在 applicationDbContext.cs 文件中添加要添加到身份用户表的字段,如下所示:

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

You have to add the fields you want to add to the identity users table in the applicationDbContext.cs file like this:

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; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文