Lock
Lock

Reputation: 5522

Why do Users and Roles for ASP.NET identity not appear in Db Context

I am using ASP.NET Identity 2.0 which from what I understand uses the Entity Framework. I am curious as to why there are no DbSet<ApplicationUser> properties set on the DB context? How does this still use the same context if they are not literally specified on the context. Do these sit on the base class?

I want to write a Linq query that goes from ApplicationUser -> ApplicationRoles -> ApplicationRole (which is a self referencing property) and want to use Linq to Entities for this, however I can't find much on this on Google and want to ensure my context is correct before writing the Linq query.

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

ApplicationRole.cs

public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }

    public ApplicationRole(string name)
        : base(name)
    { }

    public ApplicationRole(string name, string description)
        : base(name)
    {
        this.Description = description;
    }

    public string Description { get; set; }

    public virtual ApplicationRole ParentRole { get; set; }
}

Upvotes: 1

Views: 745

Answers (1)

jakejgordon
jakejgordon

Reputation: 4098

I'm guessing you have your own DbContext and you just need to extend IdentityDbContext like this:

public class MyDbContext: IdentityDbContext<ApplicationUser>

Once you extend this then you should have the Identity classes available on your DbContext (in addition to the other stuff that's already in your database).

Upvotes: 1

Related Questions