locked
Error: EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. RRS feed

  • Question

  • User-953356284 posted

    Hi I have created a web application using Entity Framework code first.

    I'm trying to link one of my models to the UserIdentity login facility in which ASP provide. 

    I currently have this in my model....

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace LJLCV.Models
    {
        public class cvdetail
        {
            ////////////////////////////////////////////////////////// 
    
            [Key, ForeignKey("ApplicationUser")]
            public string UserId { get; set; }
            public virtual ApplicationUser ApplicationUser { get; set; }
     
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "First Name is required"), StringLength(100), Display(Name = "First Name")]
            public string FirstName { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Last Name is required"), StringLength(100), Display(Name = "Last Name")]
            public string LastName { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Address is required"), StringLength(300), Display(Name = "Address"), DataType(DataType.MultilineText)]
            public string Address { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Landline is required"), StringLength(12), Display(Name = "LandLine")]
            public string LandLine { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Mobile is required"), StringLength(12), Display(Name = "Mobile")]
            public string Mobile { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Email Address is required"), Display(Name = "Email Address"), DataType(DataType.EmailAddress)]
            [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
                                ErrorMessage = "Email Address is is not valid.")]
            public string Email { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "A personal statement is required"), StringLength(10000), Display(Name = "Personal Statement"), DataType(DataType.MultilineText)]
            public string PersonalStatement { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Skils are required"), StringLength(500), Display(Name = "Skills"), DataType(DataType.MultilineText)]
            public string Skills { get; set; }
    
            ////////////////////////////////////////////////////////// 
    
            [Required(ErrorMessage = "Interests & Hobbies are required"), StringLength(50), Display(Name = "Interests & Hobbies "), DataType(DataType.MultilineText)]
            public string InterestsnHobbies { get; set; }
    
            //////////////////////////////////////////////////////////
    
        }
    }

    But I'm getting this error:

    <!--StartFragment-->

    LJLCV.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
    LJLCV.Models.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.

    Anyone able to help? 

    <!--EndFragment-->

    Friday, January 1, 2016 4:23 PM

Answers

  • User614698185 posted

    Hi LukeHarris,

    Your code doesn't show this, but from the errors you are getting I assume that you are overriding OnModelCreating. This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // your stuff here
    }

    Or you do the mapping:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    }

    For more information, please see:

    http://forums.asp.net/t/2050505.aspx?Code+first+EntityType+IdentityUserLogin+has+no+key+defined+

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 4, 2016 5:57 AM

All replies

  • User614698185 posted

    Hi LukeHarris,

    Your code doesn't show this, but from the errors you are getting I assume that you are overriding OnModelCreating. This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // your stuff here
    }

    Or you do the mapping:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
    }

    For more information, please see:

    http://forums.asp.net/t/2050505.aspx?Code+first+EntityType+IdentityUserLogin+has+no+key+defined+

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 4, 2016 5:57 AM
  • User-953356284 posted

    Okay thanks very much for the reply!

    I have done that. However, it's now duplicating the tables and linking CVDetails to 'ApplicationUsers' and still creating 'AspNetUsers' and its relevant tables where the data is being stored.

    I want CVDetails to link to AspNetUsers.

    Any idea how to resolve this? :)

    Many Thanks

    Luke

    Monday, January 4, 2016 2:57 PM
  • User614698185 posted

    Hi LukeHarris,

    It seems you have solved your original issue. And you have post this question to a new thread: http://forums.asp.net/p/2081683/6006563.aspx?Duplicating+of+tables+AspNetUsers+ApplicationUsers

    In this thread, Dillion has given your answer.

    Best Regards,

    Candice Zhou

    Wednesday, January 6, 2016 7:44 AM