7

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

Community
  • 1
  • 1
Tim
  • 7,401
  • 13
  • 61
  • 102
  • generally speaking you don't want to use the same dbContext for your application models and the identity models, the reason is not being able to control the async nature of commits, for example the user manager from the identity might save changes in the middle of your business logic working with the same context, however if your model is also code first then you should be able to merge the two – Kris Ivanov Nov 15 '13 at 14:52

2 Answers2

3

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.

user1089766
  • 597
  • 6
  • 14
2

I recommend using those dbContext separate, due to async nature of how identity works. You want to have absolute control over your application context.

For that reason I usually inject the identity dbContext by using the same connection from the application context, but there are two separate instances.

Also if you ever wanted to have your application dbContext anything other than code first it will not be possible to merge with the identity dbContext.

Kris Ivanov
  • 10,476
  • 1
  • 24
  • 35
  • So what method would you use to navigate from the currently logged on user to other linked entities in your EF model. i.e. when i userA clicks on a link to load a new page, the controller method has a User Property which shows his logged on user, i want to be able to do a User.Orders.First().Id forexample – Tim Nov 15 '13 at 15:34
  • 1
    depending on how you have your relationships setup, you should go against your application model repository and load orders using the user id, something like `dbContext.GetDbSet.Where(o => o.UserId == User.Identity.GetUserId()).Select(o => o.Id).First()` it just depends on your model – Kris Ivanov Nov 15 '13 at 17:00
  • 1
    Although I like using Code First most of the time. However, sometimes it's easier for me to reverse POCO a table that has data rather than writing a seed method. – stink Nov 15 '13 at 22:47