K.Z
K.Z

Reputation: 5075

Getting error unable to determine composite primary key while creating strongly typed view

I am working on ASP.NET-MVC application, I am trying to create strongly typed view from controller but I am getting following Error as show in diagram

enter image description here

enter image description here

Model class

public class PropertyRentingApplication
{
     public PropertyRentingApplication() { }


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

     [Key, ForeignKey("PropertyType")]
     [Display(Name = "Property Type ID")]
     [Required(ErrorMessage = "Require Property Type ID")]
     public int PropertyTypeID { get; set; }

     [Key, ForeignKey("Student")]
     [Display(Name = "Student ID")]
     [Required(ErrorMessage = "Require Student ID")]
     public int StudentID { get; set; }

     [Display(Name = "Application Reference")]
     [MaxLength(150)]
     [Required(ErrorMessage = "Application Reference")]
     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; }

}

I have updated as below " [Column(Order = 1)]" but is still throughing same error

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

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

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

Upvotes: 1

Views: 91

Answers (1)

marc_s
marc_s

Reputation: 754868

You need to decorate your key columns with [Column(Order = 0)], [Column(Order = 1)] and so forth, to define what order these columns appear in in the key

Upvotes: 2

Related Questions