I'm developing a simple MVC 5 application using Entity Framework 6 in Visual Studio 2013. I want to associate users (from ASP.NET Identity 2.0) with my own models in the following manner:
There are two types of users (roles), an author and a grader. The author posts an article and the grader generates a grade report for such article. Any grader can grade any ungraded article. The author will be able to see who graded their articles, so I'm trying to associate 2 users with an article (as well as with grade reports).
I want to know if I'm on the right direction or if I should add or modify something. I've read something about overriding the onModelCreating method so I don't get some errors related with keys, but I don't understand that very well.
These are my models. As you can see, there's a one-to-zero-or-one relationship between an Article and a GradeReport. A GradeReport only exists in relation to one Article.
public class Article
{
public int ArticleID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
public bool Status { get; set; } // To tell if it has been graded or not
public string AuthorID { get; set; }
public string? GraderID { get; set; }
public int? GradeReportID { get; set; }
public virtual ApplicationUser Author { get; set; }
public virtual ApplicationUser Grader { get; set; }
public virtual GradeReport GradeReport { get; set; }
}
public class GradeReport
{
[Key, ForeignKey("Article")]
public int ArticleID { get; set;}
public DateTime Date { get; set; }
public string Grade { get; set; }
public string Comments { get; set; }
public string GraderID { get; set; }
public string AuthorID { get; set; }
public virtual Article Article { get; set; }
public virtual ApplicationUser Grader { get; set; }
public virtual ApplicationUser Author { get; set; }
}
This is my ApplicationUser class. I added some custom properties which include the articles and grade reports for a particular user.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Article> Articles { get; set; }
public virtual ICollection<GradeReport> GradeReports { get; set; }
}
This is my DB context which extends IdentityDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
static ApplicationDbContext()
{
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
DbSet<Article> Articles { get; set; }
DbSet<GradeReport> GradeReports { get; set; }
}
Am I doing it right? Is there a better way to associate users with models?