EntityType 'IdentityUserLogin' has no defined key. Define the key for this entity type

advertisements

I am getting these errors when trying to create a merchant.

FlavorPing.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.

FlavorPing.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.

UserLogins: EntityType: EntitySet 'UserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

UserRoles: EntityType: EntitySet 'UserRoles' is based on type 'IdentityUserRole' that has no keys defined."

Here is my merchant model:

namespace FlavorPing.Models
{
    public class Merchant
    {
        //Meant to inherit identity.
        //[ForeignKey("ApplicationUserId")]
        public string ApplicationUserId { get; set; }
        [ForeignKey("ApplicationUser")]
        public virtual List<ApplicationUser> ApplicationUser { get; set; }

        [Key]
        public int MerchantID { get; set; }

        [Required]
        [Display(Name = "Business Name")]
        public string MerchantName { get; set; }

        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string email { get; set; }

        //need to create formatting here.
        [Required]
        [Display(Name = "Web Site Link")]
        public string website { get; set; }

        //public int MenuItemID { get; set; }

        public virtual List<MenuItem> MenuItems { get; set; }

        public virtual MerchantDetails MerchantDetails { get; set; }

        public ICollection<FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; }
    }
}

Here is the create controller for merchant, which is where I am getting the error:

// POST: Merchants/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MerchantID,MerchantName,email,website")] Merchant merchant)
{
    if (ModelState.IsValid)
    {
        merchant.ApplicationUserId = User.Identity.GetUserId();
        db.Merchants.Add(merchant);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(merchant);
}

Here is my DBContext:

namespace FlavorPing.Models
{
    public class FlavorPingContext : IdentityDbContext
    {

        public FlavorPingContext()
            : base("name=FlavorPingContext")
        {
        }

        public System.Data.Entity.DbSet<FlavorPing.Models.Merchant> Merchants { get; set; }

        public System.Data.Entity.DbSet<FlavorPing.Models.MenuItem> MenuItems { get; set; }

        public System.Data.Entity.DbSet<FlavorPing.Models.MerchantDetails> MerchantDetails { get; set; }

        public System.Data.Entity.DbSet<FlavorPing.Models.Follower> Followers { get; set; }

        public System.Data.Entity.DbSet<FlavorPing.Models.FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; }

        public DbSet<IdentityUserLogin> UserLogins { get; set; }
        public DbSet<IdentityUserClaim> UserClaims { get; set; }
        public DbSet<IdentityUserRole> UserRoles { get; set; }

        protected override void OnModelCreating(DbModelBuilder builder)
        {
            // Primary keys
            builder.Entity<Follower>().HasKey(q => q.FollowerID);
            builder.Entity<MenuItem>().HasKey(q => q.MenuItemID);
            builder.Entity<Merchant>().HasKey(q => q.MerchantID);
            builder.Entity<FollowerMenuItemMerchant>().HasKey(q =>
                new
                {
                    q.FollowerID,
                    q.MenuItemID,
                    q.MerchantID
                });

            // Relationships
            builder.Entity<FollowerMenuItemMerchant>()
                .HasRequired(t => t.Follower)
                .WithMany(t => t.FollowerMenuItemMerchants)
                .HasForeignKey(t => t.FollowerID);

            builder.Entity<FollowerMenuItemMerchant>()
                .HasRequired(t => t.MenuItem)
                .WithMany(t => t.FollowerMenuItemMerchants)
                .HasForeignKey(t => t.MenuItemID);

            builder.Entity<FollowerMenuItemMerchant>()
            .HasRequired(t => t.Merchant)
            .WithMany(t => t.FollowerMenuItemMerchants)
            .HasForeignKey(t => t.MerchantID);

            builder.Conventions.Remove<PluralizingTableNameConvention>();
            builder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        }
    }
}

I am trying to follow the example (option2) in this link: EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

I am trying Option 2 because I want to avoid having two DB's. But I am new to managing a DB so if you think I should do Option 3 please advise as to why, or if you see why I am getting this error please tell me why. Thanks in advance!


Ok I fixed my issue by adding this into my DBContext class.

    builder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    builder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    builder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });