MVC5 Identity + Multiple Context in EntityFramework
I have an MVC 5 application that uses default authentication. The user profile is part of our model, which means there are several classes that have a foreign key to UserInfo. There are 2 DbContext, one for the model and one for the UserInfo.
public class ApplicationDbContext : IdentityDbContext<UserInfo>
{
public ApplicationDbContext()
: base("Profile")
{
}
}
public class UserInfo : IdentityUser
{
public UserInfo ()
{
LibraryItems = new List<LibraryItem>();
if (UserGroup == UserGroupEnum.ANALYST)
{
Companies = new List<Company>();
}
DateCreate = DateTime.Now;
DateUpdate = DateTime.Now;
DateDelete = DateTime.Now;
}
[DisplayColumnInIndex]
[DisplayName("Is Active ?")]
public bool IsActive { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
//[ValidateFile(ErrorMessage = "Please select a PNG image smaller than 1MB")]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Editable(false, AllowInitialValue = true)]
public DateTime DateCreate { get; set; }
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateUpdate { get; set; }
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateDelete { get; set; }
[DisplayColumnInIndex]
public String Name { get; set; }
[DisplayColumnInIndex]
public String FirstName { get; set; }
[DisplayColumnInIndex]
public String Pseudo { get; set; }
[DisplayColumnInIndex]
public string PhoneNumber { get; set; }
[DisplayColumnInIndex]
public String Company { get; set; }
[DisplayName("Name_Id")]
[ForeignKey("FullName")]
public int? NameId { get; set; }
[DisplayColumnInIndex]
public UserGroupEnum UserGroup { get; set; }
[DisplayColumnInIndex]
public UserStatusEnum UserStatus { get; set; }
public virtual Name FullName { get; set; }
}
public class Comment
{
// more properties
[DisplayName("UserInfoId")]
[ForeignKey("UserInfo")]
public virtual String UserInfoId { get; set; }
}
public class Like
{
// more properties
[DisplayName("UserInfoId")]
[ForeignKey("UserInfo")]
public virtual String UserInfoId { get; set; }
}
My userInfo also has foreign keys to other tables in the second DBontext.
What is the best way to design our authentication system? and maintain a link between the two contexts.
Thanks in advance.
source to share
I recently came up with a solution to use a single context for both ASP.NET credentials 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
inherits from IdentityDbContext<UserInfo>
.
There are some trade-offs with this approach: for example, your data access layer must reference Microsoft.AspNet.Identity.Core
and Microsoft.AspNet.Identity.EntityFramework
; however, having a single database context in your project makes things easier if you are using dependency migration or Entity Framework migrations.
source to share