0
votes

ASP.NET MVC 5, I am having some issues with adding migration when involving entities that are referencing the ApplicationUser entity. I am using individual account for authentication when created the project.

public class Blog
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [ForeignKey("ApplicationUser")]
    public string ApplicationUserId { get; set; }

    public ApplicationUser ApplicationUser { get; set; }

    [Required]
    [StringLength(500, ErrorMessage = "The {0} must be at least {2} characters long.")]
    public string Content { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public bool IsSoftDeleted { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

And my DBContexts is

public class ApplicationUserDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationUserDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationUserDbContext Create()
    {
        return new ApplicationUserDbContext();
    }
}

public class BloggingServiceDbContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    public BloggingServiceDbContext() : base("DefaultConnection")
    {
    }
}

The script I have run on package management console is:

Add-Migration InitialBlogMigration -ConfigurationTypeName BloggerConfiguration

The error I get is:

BloggingService.Web.Context.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
BloggingService.Web.Context.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.

Note: before implemented Blog, there was no issue with running add-migration and the database do not contain any of the entities mentioned in the error message.

I have run out of clues on what could be the issue. Can you please help me spot the issue?

1

1 Answers

0
votes

The resolution seems to be simple, I need to place all entities where referencing ApplicationUser to the ApplicationUserDbContext. But I still don't know why this is the case, would be nice if someone could clarify this for me.