【问题标题】:Reading Identity Users Information From EF in ASP.NET Core从 ASP.NET Core 中的 EF 读取身份用户信息
【发布时间】:2017-08-28 13:24:24
【问题描述】:

我已经像这样扩展了默认的 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 访问用户实体。

这样做的正确方法是什么?

【问题讨论】:

    标签: c# entity-framework asp.net-core


    【解决方案1】:

    ApplicationDbContext是IdentityDbContext&lt;ApplicationUser&gt;,你可以自己写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();
    

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2020-01-20
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多