I'm learning to build a ASP.NET MVC Core web application. Everything works fine until I start to add Identity to it.
Registered User can create many Jobs. I use the ApplicationUser.cs that comes with the project for the User. I created another DbContext for all others entities.
ApplicationUser.cs in Model:
public class ApplicationUser : IdentityUser
{
// CUSTOM PROPERTIES
public string Name { get; set; }
public ICollection<Job> Jobs { get; set; }
}
Job.cs in Model:
public class Job
{
public int ID { get; set; }
public string Title { get; set; }
// contains other properties
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
ApplicationDbContext.cs:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
When I add these 2 lines in Job.cs, the error pops up.
public string ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
Error ==>>
Unhandled Exception: System.Exception: Could not resolve a service of type 'MyProject.Data.JobDbContext' for the parameter 'context' of method 'Configure' on type 'MyProject.Startup'. ---> System.InvalidOperationException: The entity type 'IdentityUserLogin' requires a primary key to be defined.
I know that there are some threads discussing about the same error, but those answer can't really help.
base.OnModelCreatingstatement toOnModelCreatingmethod could solve the problem but it doesn't in my code.ApplicantionUsermodel is derived fromIdentityUser. Isn't it got theUserIdPK inherited fromIdentityUser? I tried to addpublic int ApplicationUserId { get; set; }to theApplicationUserbut still showing the same problem...