1 Replies - 2466 Views - Last Post: 28 June 2016 - 07:36 AM

#1 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 579
  • Joined: 08-July 13

EntityType 'IdentityUserLogin' has no key defined. Define the

Posted 28 December 2015 - 07:58 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: http://stackoverflow...-for-this-entit

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.

This post has been edited by jphoc13: 28 December 2015 - 08:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: EntityType 'IdentityUserLogin' has no key defined. Define the

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: EntityType 'IdentityUserLogin' has no key defined. Define the

Posted 28 June 2016 - 07:36 AM

Does your actual DB table have a primary key defined? Did you create the tables using Code-First or was this a Database First approach? At first-glance it looks like Code First, but I just want to make sure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1