繁体   English   中英

在ASP.NET Core中从EF读取身份用户信息

[英]Reading Identity Users Information From EF in ASP.NET Core

我已经扩展了默认的asp.net核心身份用户,如下所示:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public MyObject SomeObject { get; set; }
    public List<Email> AssociatedEmails { get; set; }
}

似乎所有用户数据都存储在名为AspNetusers的表中,但是在ApplicationDbContext中对此没有定义,因此我无法在我的代码中引用它。

我需要能够从Entity Framework中读取各种用户详细信息,并将用户记录与各种其他实体相关联,因此我需要能够通过EF访问用户实体。

正确的方法是什么?

ApplicationDbContext是一个IdentityDbContext<ApplicationUser> ,您可以编写自己的IdentityDbContext

public class YourApplicationDbContext : IdentityDbContext<YourApplicationUser>
{
    public YourApplicationDbContext (DbContextOptions options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

并在启动类中使用它

services.AddDbContext<YourApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<YourApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<YourApplicationDbContext>()
                .AddDefaultTokenProviders();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM