2017-08-28 99 views
2

我已经扩展了默认的asp.net核心用户身份的像这样:在ASP.NET核心阅读用户身份信息从EF

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访问用户实体。

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

回答

0

ApplicationDbContextIdentityDbContext<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();