locked
EntityType 'CustomUserLogin' has no key defined. Define the key for this EntityType RRS feed

  • Question

  • User-745958333 posted

    Hi.

    I've just started a new project in Web Forms because I thought it'd be quicker than learning MVC, but how wrong was I!  I'm struggling with the Identity aspect of my project.  I've followed this link here: http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity

    And it all compiles ok and works (my ID column is now an integer).

    So I've created a new class for a 'Property' context:

    using System.Collections.Generic;
    using System.Data.Entity;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.Owin;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System.Web.Security;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.ModelConfiguration.Conventions;
    
    namespace MyApp.Models
    {
        public class PropertyContext : DbContext
        {
            public PropertyContext()
                : base("DefaultConnection")
            {
            }
            public DbSet<Property> Property { get; set; }      
    
        }
    
        public class Property
        {
            [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), ScaffoldColumn(false)]
            public int PropertyID { get; set; }
    
            //[Required, StringLength(128)]
            //public string OwnerID { get; set; }
    
          //  [ForeignKey("OwnerID")]
            //public virtual ApplicationUser ApplicationUser { get; set; }
            
            //[ForeignKey("OwnerID")]
            //public int OwnerID { get; set; }
           // public virtual ApplicationUser ApplicationUser { get; set; }
    
            
            public int OwnerID { get; set; }
            [ForeignKey("OwnerID")]
            public virtual ApplicationUser ID { get; set; }
    
    
            [Required, StringLength(255), Display(Name = "Address 1")]
            public string Address1 { get; set; }
            [Required, StringLength(255), Display(Name = "Address 2")]
            public string Address2 { get; set; }
            [Required, StringLength(255), Display(Name = "Address 3")]
            public string Address3 { get; set; }
            [Required, StringLength(255), Display(Name = "Town/City")]
            public string Settlement { get; set; }
            [Required, StringLength(255), Display(Name = "County")]
            public string County { get; set; }
            [Required, StringLength(255), Display(Name = "Post Code")]
            public string PostCode { get; set; }
            [Required, StringLength(255), Display(Name = "Country")]
            public string Country { get; set; }
    
        }
    
    }

    Whereby I'm trying to make a new int column called 'OwnerID' a foreign key into the 'AspNetUser' table's ID column.  When i build my project, it builds fine.  However when I try to add a migration I get this:

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

    So in my ApplicationDbContext I've added this:

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
                base.OnModelCreating(modelBuilder);
    
                modelBuilder.Entity<ApplicationUser>().ToTable("Users").HasKey<int>(u => u.Id);
                modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles").HasKey(ur => new { ur.RoleId, ur.UserId });
                modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins").HasKey<int>(ul => ul.UserId);
                modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims").HasKey<int>(uc => uc.Id);
                modelBuilder.Entity<CustomRole>().ToTable("Roles").HasKey<int>(r => r.Id);
    
    
    
            }

    But I still get the same issue?  Can somebody help me here?  I have no idea what's going on tbh....  Thanks in advance.

    UPDATE:

    The issue arises with the following code (when I try to configure the primary key!)

    public int OwnerID { get; set; }
    [ForeignKey("OwnerID")]
    public virtual ApplicationUser ID { get; set; }
    Saturday, August 1, 2015 9:46 AM

Answers

  • User-84896714 posted

    Hi Captain Planet,

    Thank you for your post. Why you put ApplicationUser and Property entities in different contexts? If they contains FK reference, they must in same context. Move below code to ApplicationDbContext.

    public DbSet<Property> Propertys { get; set; }     

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 1:17 AM

All replies

  • User-84896714 posted

    Hi Captain Planet,

    Thank you for your post. Why you put ApplicationUser and Property entities in different contexts? If they contains FK reference, they must in same context. Move below code to ApplicationDbContext.

    public DbSet<Property> Propertys { get; set; }     

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 1:17 AM
  • User-745958333 posted

    Ahh that makes sense.  Sorry, I'm just trying to get my head around all these new concepts.

    One other query if I may:

    I'm attempting to rename my AspNetUsers table to 'User', and the 'Id' column to 'UserId' by adding the following to OnModelCreating:

        modelBuilder.Entity<ApplicationUser>().ToTable("User").Property(p => p.Id).HasColumnName("UserId");

    For my foreign key in the Property entity, I've used this:

    [ForeignKey("UserId")]
    public int OwnerID { get; set; }

    public virtual ApplicationUser ApplicationUsers { get; set; }

    I then delete the local MDF so it rebuilds when I next access the database.  When I then try to register a new user i get the exception:

    The ForeignKeyAttribute on property 'OwnerID' on type 'MyApp.Models.Property' is not valid. The navigation property 'UserId' was not found on the dependent 

    Am i renaming the column at the right time, and in the right way?

    Thanks....

    Monday, August 3, 2015 7:10 AM
  • User-745958333 posted

    This post provides a possible solution:

    http://stackoverflow.com/questions/24475351/asp-identity-table-column-change-but-mapping-is-not-possible-with-new-column-nam

    Which got me wondering.  Am I best defining ALL of my relationships using Fluent API as opposed to a mixture of Fluent API and Data Annotations? (Hope I'm getting my terminology right here....)

    Monday, August 3, 2015 8:31 AM
  • User-84896714 posted

    Hi Captain,

    There are two main ways you can configure EF to use something other than conventions, namely  annotations or EFs fluent API. The annotations only cover a subset of the fluent API functionality, so there are mapping scenarios that cannot be achieved using annotations. This article is designed to demonstrate how to use the fluent API to configure properties.

    It means everything what you can configure with DataAnnotations is also possible with the Fluent API. The reverse is not true.  So it' better use Fluent API for better flexibility.

    In addition, it' better to create a new thread when you want to ask a new question.

    Best Regards,
    Wang Li

    Monday, August 3, 2015 9:12 PM