Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using same dbContext for Identity and other db entities

I have an application which uses EntityFramework edmx models and i want to be able to use the same dbContext for my Identity classes and the entity classes. Someone has raised a Similar Query but i am unable to get them to be compatible.

ive changed the class definition in the EF context class as below

    public partial class MyDbContext : IdentityDbContext<AspNetUser>
    {
    }

and my identity user as

public partial class AspNetUser : IdentityUser
{
}

but i get an error when i try to login or register

The entity type AspNetUser is not part of the model for the current context

like image 946
Tim Avatar asked Nov 15 '13 13:11

Tim


1 Answers

The solution I came up with recently is to use single context for both ASP.NET identity data and your business entities:

public class DatabaseContext : IdentityDbContext<UserInfo>
{
    public virtual DbSet<Comment> Comments { get; set; } // Your business entities

    public DatabaseContext()
    : base("name=DatabaseContext")
     {
     }
}

Notice that the DatabaseContext inherits from the IdentityDbContext.

There are some trade-offs with this approach: for example, your data access layer should reference Microsoft.AspNet.Identity.Core and Microsoft.AspNet.Identity.EntityFramework; however, having a single database context in your project makes things much easier if you are using dependency injection or Entity Framework migrations.

like image 163
user1089766 Avatar answered Sep 28 '22 21:09

user1089766