I am using the default ASP.Net ASP.NET template in Visual Studio. I use the ASP.Net Identity code that was created for me in the template. For the DBC context, I would like to use information about the relationship between the ApplicationUser entity (AspNetUser table) and the rest of my entities. For example, I want to have an ApplicationUser.Messages property that shows the relationship between ApplicationUser and Message objects. I have a DbContext for all non-identical objects in a Data Access Layer project. And the ApplicationDbContext template is in the user interface layer. To maintain the relationship between Identity objects and my custom objects, I need to merge into one DbContext, right? How can I do it?
Here is an example of the code that I have:
The IdentityUser and DbContext project created for me in the UI Layer project from the MVC template with my Messages custom property:
public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); // Add custom user claims here return userIdentity; } public ICollection<Message> Messages { get; set; } } public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } }
The message class that I have in my domain / business logic:
public class Message { public int Id { get; set; } [Required] public string Title { get; set; } [Required] public string Body { get; set; } [Required] public DateTime Created { get; set; } }
DBContext that I have in my Data Access Layer project:
public class PSNContext : DbContext, IPSNContext { public PSNContext() :base ("DefaultConnection") { } public DbSet<Message> Messages { get; set; } }
It is wrong to bring user interface code like this from ApplicationUser in the user interface layer to my business logic level:
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
Is there a better way to do this?
source share