【问题标题】:Getting error unable to determine composite primary key while creating strongly typed view创建强类型视图时出现无法确定复合主键的错误
【发布时间】:2015-07-27 13:58:53
【问题描述】:

我正在开发 ASP.NET-MVC 应用程序,我正在尝试从控制器创建强类型视图,但我收到以下错误,如图所示

模型类

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; }

}

我已更新如下“[Column(Order = 1)]”,但仍然遇到同样的错误

[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; }

【问题讨论】:

  • 您需要使用[Column(Order = 0)] 等来装饰您的键列,以定义这些列在键中出现的顺序
  • 不知道是否重要,但在您的代码中,StudentID 被标记为键,但不在您的列截图中。
  • @marc_s:请将其发布为答案,因为它就是答案。否则,OP 不能接受它,这个问题将永远保持“未回答”。
  • 请在底部查看我的问题,我已添加 [Column(Order = 0)] 但仍然出现错误!!!!!!

标签: c# asp.net-mvc-5 entity-framework-6 data-annotations


【解决方案1】:

您需要使用[Column(Order = 0)][Column(Order = 1)] 等来装饰您的键列,以定义这些列在键中出现的顺序

【讨论】:

    猜你喜欢
    • 2015-12-17
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多