1

I am building an application in c# using ASP.NET MVC 5/Entity 6 frameworks with code first approach. I changed the Id attribute in Identity 2 from string to integer using the documentation.

Then I create the migration using the following command

Add-Migration InitialCreate

The migration failed to add due to the following errors

DbContexts.CustomUserLogin: : EntityType 'CustomUserLogin' has no key defined. Define the key for this EntityType.
DbContexts.CustomUserRole: : EntityType 'CustomUserRole' has no key defined. Define the key for this EntityType.
CustomUserLogins: EntityType: EntitySet 'CustomUserLogins' is based on type 'CustomUserLogin' that has no keys defined.
CustomUserRoles: EntityType: EntitySet 'CustomUserRoles' is based on type 'CustomUserRole' that has no keys defined.

I made some changes to the ApplicationUser class by adding more Foreign Keys and some relations like so

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Proj1.Areas.Base.Models;
using System.Collections.Generic;

namespace Proj1.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> 
    {
        [ForeignKey("Role")]
        public int RoleId { get; set; }

        [ForeignKey("Client")]
        public int CurrentClientId { get; set; }

        [ForeignKey("Campaign")]
        public int? CurrentCampaignId { get; set; }

        public virtual Role Role { get; set; }

        public virtual Client Client { get; set; }

        public virtual Campaign Campaign { get; set; }

        public virtual List<UserToPrivilege> UserToPrivileges { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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 class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

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

    }

    public class CustomUserRole : IdentityUserRole<int> { }
    public class CustomUserClaim : IdentityUserClaim<int> { }
    public class CustomUserLogin : IdentityUserLogin<int> { }

    public class CustomRole : IdentityRole<int, CustomUserRole>
    {
        public CustomRole() { }
        public CustomRole(string name) { Name = name; }
    }

    public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
        CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public CustomUserStore(ApplicationDbContext context)
            : base(context)
        {
        }
    }

    public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
    {
        public CustomRoleStore(ApplicationDbContext context)
            : base(context)
        {
        }
    } 
}

What is the issue here? How can I fix it?

Junior
  • 11,602
  • 27
  • 106
  • 212
  • Could you post the code of the classes which are causing the error ? – PhillipH Jun 09 '16 at 17:24
  • I update my question – Junior Jun 09 '16 at 17:29
  • I tried this for one project, then realized it wasn't worth the effort to change the ids from string to int. – jrummell Jun 09 '16 at 17:45
  • You're missing some important information. So you tried to generate the migration, it failed with the error you posted. You made changes to the `ApplicationUser` and then what? Where should be any issue? Are you trying to generate the migration again? Did it fail again? What errors did you get? – David Ferenczy Rogožan Jun 09 '16 at 17:47
  • After changing the Identity attribute from string to int like the doc. I added the attributes to the `ApplicationUser` just like my code above. Then I tried to Add-Migration and that is when I got the error. So the migration is not generating do the error I stated in my questions – Junior Jun 09 '16 at 17:50
  • 1
    Possible duplicate of [EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType](http://stackoverflow.com/questions/28531201/entitytype-identityuserlogin-has-no-key-defined-define-the-key-for-this-entit) – trailmax Jun 10 '16 at 09:18

0 Answers0