Reputation: 1165
Is it possible to tell asp identity to use the same dbcontext as application dbcontext? My tables need to relate to identity table. Each table has createuser and edituser field that linked to identity table. If asp use its own dbcontext, then i have to also add entities and mapping to identity tables to the application dbcontext. Is the a better approach for this situation? I use entity framework code first 6 and and asp mvc 5.
EDIT
Based on answers below, i can use the same dbcontext as long as my dbcontext inherited from IdentityContext.
My question:
Do i still need to create entity class and map manually? How do i map relations between my tables to identityuser?
This is my current dbcontext
public partial class MyContext : DbContext
{
static MyContext()
{
Database.SetInitializer<MyContext>(null);
}
public MyContext()
: base("Name=MyContext")
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
}
public new DbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
{
return base.Set<TEntity>();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new AspNetRoleMap());
modelBuilder.Configurations.Add(new AspNetUserClaimMap());
modelBuilder.Configurations.Add(new AspNetUserLoginMap());
modelBuilder.Configurations.Add(new AspNetUserMap());
modelBuilder.Configurations.Add(new PersonMap());
}
Upvotes: 2
Views: 3356
Reputation: 105
In EF 6.0, the better way to go will be to have two separate Contexts and then use the same connection string and 2 different migration configurations as per Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations.
Upvotes: 0
Reputation: 108
My question:
Do i still need to create entity class and map manually? How do i map relations between my tables to identityuser?
No, all of your Identity classes are already mapped. If you want to add relationship to your Identity, you can do that as you would normally in the OnModelCreating method in the context itself reffering to your Identity classes(e.g. IdentityRole, IdentityUserRole, IdentityUser).
For more information on how this is done, take a look at this article.
Upvotes: 0
Reputation: 11554
Your context class must be inherited form IdentityDbContext
instead of DbContext. IdentityDbContext
contains some configuration for IdentityUser, IdentityRole, IdentityUserLogin, IdentityUserRole, IdentityUserClaim
and it has IDbSet Users
and IDbSet Roles
.
public class MyContext : IdentityDbContext<IdentityUser or your own UserEntity>
{
.
.
.
}
Upvotes: 1
Reputation: 9411
Sure
When you create an instance of your User Manager just pass over the User Store along with your own dbContext.
new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()))
Upvotes: 0