简体   繁体   中英

Reading Identity Users Information From EF in ASP.NET Core

I have extended the default asp.net core identity user like so:

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

It seems that all user data is stored in a table called AspNetusers but there is no definition for this in the ApplicationDbContext so i can't reference it in my code.

I need to be able to read various users details from Entity Framework and also associate users records with various other entities so i need to be able to access the user entity through EF.

What is the proper way to do this?

ApplicationDbContext is an IdentityDbContext<ApplicationUser> , you can write your own 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);
    }
}

and use it in your startup class

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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