Using the same dbContext for Identity and other db objects

I have an application that uses EntityFramework edmx models, and I want to be able to use the same dbContext for my Identity classes and entity classes. Someone raised Similar queries , but I can't get them to be compatible.

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

public partial class MyDbContext : IdentityDbContext<AspNetUser> { } 

and my user id

 public partial class AspNetUser : IdentityUser { } 

but I get an error when trying to login or register

AspNetUser object type is not part of the model for the current context

+7
c # visual-studio-2013 entity-framework asp.net-mvc-5 asp.net-identity
source share
2 answers

I recently came up with the decision to use a single context for both ASP.NET identity and your business units:

 public class DatabaseContext : IdentityDbContext<UserInfo> { public virtual DbSet<Comment> Comments { get; set; } // Your business entities public DatabaseContext() : base("name=DatabaseContext") { } } 

Note that DatabaseContext is inherited from IdentityDbContext.

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

+3
source share

I recommend using these dbContext separately due to the asynchronous nature of the identifier. You want absolute control over the context of your application.

For this reason, I usually insert the dbContext identifier using the same connection from the application context, but there are two separate instances.

Also, if you ever wanted your dbContext application to be something other than code, it was impossible to merge with the dbContext identifier.

+2
source share

All Articles