Search code examples
asp.net-mvc-5entity-framework-6asp.net-mvc-viewmodel

asp.net mvc viewmodel entity has no key Error when trying to create view from controller


I have ViewModel class in ASP.NET-MVC application and when I am trying to create view --> to detail the records. I am getting error that entity don't have primary key. I have test invidual model classes and they are working fine, I am not what I am missing here...

ViewModel class

public class RentingApplicationsViewModel
{
    public RentingApplicationsViewModel() {

        _PropertyRentingApplicationModel = new PropertyRentingApplication();
        _PropertyTypeModel = new PropertyType();
    }

    public PropertyRentingApplication _PropertyRentingApplicationModel { get; set; }
    public PropertyType _PropertyTypeModel { get; set; }


}

Model class

[Table("PropertyRentingApplication")]
public class PropertyRentingApplication
{
     public PropertyRentingApplication() { }


     [Key]
    // [Column(Order = 0)]
     [Display(Name = "Application ID")]
     public int ApplicationID { get; set; }

     //[Key]
     //[Column(Order = 1)]
     [Display(Name = "Property Type ID")]
     [Required(ErrorMessage = "Require Property Type ID")]
     public int PropertyTypeID { get; set; }

     //[Key]
     //[Column(Order = 2)]
     [Display(Name = "Student ID")]
     [Required(ErrorMessage = "Require Student ID")]
     public int StudentID { get; set; }

     [Display(Name = "Application Reference")]
     [MaxLength(150)]
     public string ApplicationReference { get; set; }

     [Display(Name = "Date Of Application")]
     [Required(ErrorMessage = "Require Date of Application Been Submitted")]
     public System.DateTime DateOfApplication { get; set; }

     [Display(Name = "Secure Entire Property")]
     [Required(ErrorMessage = "Require Information on If You Want to Secure Entire Property")]
     public bool SecureEntireProperty { get; set; }

     [Display(Name = "Application Status")]
     [MaxLength(50)]
     [Required(ErrorMessage = "Require Application Status")]
     public string ApplicationStatus { get; set; }

     public PropertyType PropertyType { get; set; }
     public Student Student { get; set; }

     public ICollection<AdditionalTenant> AdditionalTenants { get; set; }

}

....

[Table("PropertyType")]
public class PropertyType
{
     public PropertyType() { }

     [Key]
     [Display(Name = "Property Type ID")]
     public int PropertyTypeID { get; set; }

     [Display(Name = "Property Type")]
     [MaxLength(250)]
     [Required(ErrorMessage = "Require Property Type i.e. Flat, House, Studio")]
     public string Type { get; set; }

     [Display(Name = "Title")]
     [MaxLength(250)]
     [Required(ErrorMessage = "Require Property Type Title")]
     public string Title { get; set; }

     public ICollection<Property> Properties { get; set; }
     public ICollection<PropertyRentingPrice> PropertyRentingPrices { get; set; }

}

Solution

  • Are you connecting to the Default context that includes Identity classes, If so the scaffolding is looking at every class in the identity context, You should try and create a new context for your class or you can place [key] above the user guid.