Why is Asp.Net Identity IdentityDbContext a Black-Box?
The ApplicationDbContext
's Users
and Roles
properties are mapped to the AspNetUsers
and AspNetRoles
tables, and the rest of the entities (Claims
, Logins
, UserRoles
) are mapped automatically via navigation properties. As far as I know, the prefixing of table names with "AspNet" are the only custom mappings in ApplicationDbContext
, everything else is just Entity Framework Code First conventions.
If you need direct access to the tables via the ApplicationDbContext
, you can do so like this...
using (var context = new ApplicationDbContext())
{
var users = context.Users.Include(u => u.Claims)
.Include(u => u.Logins)
.Include(u => u.Roles)
.ToList();
var roles = context.Roles.ToList();
}
You can access a user's roles, claims, and logins via navigation properties on the IdentityUser
entity (from the Users
DbSet
). If you want to query them directly, add them explicitly as DbSet
s on the context...
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<IdentityUserRole> UserRoles { get; set; }
public DbSet<IdentityUserClaim> Claims { get; set; }
public DbSet<IdentityUserLogin> Logins { get; set; }
}
And query them like this...
var claims = context.Claims.ToList();
var userRoles = context.UserRoles.ToList();
var logins = context.Logins.ToList();
ASP.NET Identity 2.0 exposes Users
and Roles
IQueryable
s on the Manager classes for convenience, but it doesn't provide any added functionality over what was available from the DbContext.
ASP.NET Identity DbContext confusion
I would use a single Context class inheriting from IdentityDbContext.
This way you can have the context be aware of any relations between your classes and the IdentityUser and Roles of the IdentityDbContext.
There is very little overhead in the IdentityDbContext, it is basically a regular DbContext with two DbSets. One for the users and one for the roles.
Asp.Net Identity - IdentityDbContextTUser causing problems for fields added to AspNetRoles table
So if you upgrade to the 2.0.0 beta packages, you should be able to get an IQueryable of ApplicationRole directly from the role manager:
roleManager.Roles
So you don't have to drop down to the DB context, this was a limitation in 1.0 that has been fixed in the 2.0 release.
Merge MyDbContext with IdentityDbContext
- Move the
ApplicationUser
definition to your DAL. - Inherit your
MyDbContext
fromIdentityDbContext<ApplicationUser>
orIdentityDbContext
- OnModelCreating - Provide the foreign key info.
- Pass
MyDbContext
while creating theUserManager<ApplicationUser>
Using ASP.net Identity user with other tables
I'm not sure what your first issue is asking. But for your second, no, you aren't required to use the context for managing other tables in your database. I use the ADO.NET Entity Data Model for my application's tables typically, and only need to use the Identity Context for any Account/Role related controllers.
ASP.NET Identity in real IT industry situations
Asp.Net Identity is the latest library by Microsoft and before it, we have been using Asp.Net Membership Provider and Web Security.
Since Asp.Net identity is built on OWIN, and working nice with Asp.Net Core, I don't see any other option to be used or offered by Microsoft.
You can use it for Forms Authentication, OAuth, Integration with Most of Social Identity Provider and Azure Active Directory.
I would say, it is the way to go
How can I change the table names when using ASP.NET Identity?
You can do this easily by modifying the IdentityModel.cs as per the below:
Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
or simply the below if you want to keep all the standard column names:
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo")
Full example below (this should be in your IdentityModel.cs file) i changed my ApplicationUser class to be called User.
public class User : IdentityUser
{
public string PasswordOld { get; set; }
public DateTime DateCreated { get; set; }
public bool Activated { get; set; }
public bool UserRole { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
modelBuilder.Entity<User>()
.ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
}
}
Please note i have not managed to get this working if the current table exists. Also note whatever columns you do not map the default ones will be created.
Third-party implementation of DbContext, how to also implement IdentityDbContext
@trailmax solution should work, but I've updated the Audit.NET library to include support for IdentityDbContext
.
A new class AuditIdentityDbContext is provided so you can change your context to inherit from that one.
You should change your code to:
public class MyDataContext : AuditIdentityDbContext<ApplicationUser>
{
...
}
Note: I'm the library owner
GitHub Issue
Related Topics
Entity Framework 4.1 Inverseproperty Attribute and Foreignkey
Solving a Timeout Error for SQL Query
Calling a SQL User-Defined Function in a Linq Query
How to Pass Variable into SQLcommand Statement and Insert into Database Table
Asp.Net MVC Using Web API to Return a Razor View
Why Does "Dtoa.C" Contain So Much Code
Linux to Windows Bad Encoding Response
Using C# 6 Features with Codedomprovider (Roslyn)
Using Ienumerable Without Foreach Loop
Why Can't I Reference System.Componentmodel.Dataannotations
Auto Create Database Tables from Objects, Entity Framework
Escaping the Escape Character Does Not Work - SQL Like Operator
Non-Virtual Interface Design Pattern in C#/C++
How to Convert Image to Data Uri for HTML with C#