Asp.net mvc 5 尝试从控制器创建视图时,asp.net mvc viewmodel实体没有关键错误

Asp.net mvc 5 尝试从控制器创建视图时,asp.net mvc viewmodel实体没有关键错误,asp.net-mvc-5,entity-framework-6,asp.net-mvc-viewmodel,Asp.net Mvc 5,Entity Framework 6,Asp.net Mvc Viewmodel,我在ASP.NET-MVC应用程序中有ViewModel类,当我试图创建view-->以详细记录时。我得到一个错误,实体没有主键。我有两个模型课程,他们工作得很好,我不是我在这里错过的 ViewModel类 模范班 [表(“PropertyRentingApplication”)] 公共类属性资源应用程序 { 公共属性EntingApplication(){} [关键] //[第列(顺序=0)] [显示(Name=“应用程序ID”)] public int ApplicationID{get;s

我在ASP.NET-MVC应用程序中有ViewModel类,当我试图创建view-->以详细记录时。我得到一个错误,实体没有主键。我有两个模型课程,他们工作得很好,我不是我在这里错过的

ViewModel类 模范班
[表(“PropertyRentingApplication”)]
公共类属性资源应用程序
{
公共属性EntingApplication(){}
[关键]
//[第列(顺序=0)]
[显示(Name=“应用程序ID”)]
public int ApplicationID{get;set;}
//[关键]
//[第列(顺序=1)]
[显示(Name=“属性类型ID”)]
[必需(ErrorMessage=“需要属性类型ID”)]
public int PropertyTypeID{get;set;}
//[关键]
//[第列(顺序=2)]
[显示(Name=“学生ID”)]
[必需(ErrorMessage=“必需学生ID”)]
公共int StudentID{get;set;}
[显示(Name=“应用程序引用”)]
[MaxLength(150)]
公共字符串应用程序引用{get;set;}
[显示(名称=“申请日期”)]
[必需(ErrorMessage=“要求提交申请日期”)]
public System.DateTime应用程序的日期{get;set;}
[显示(Name=“安全整个属性”)]
[必需(ErrorMessage=“如果要保护整个财产,则需要有关的信息”)]
公共布尔安全属性{get;set;}
[显示(Name=“应用程序状态”)]
[MaxLength(50)]
[必需(ErrorMessage=“需要应用程序状态”)]
公共字符串应用程序状态{get;set;}
公共属性类型属性类型{get;set;}
公立学生学生{get;set;}
公共ICollection附加租户{get;set;}
}

[表(“属性类型”)]
公共类属性类型
{
公共属性类型(){}
[关键]
[显示(Name=“属性类型ID”)]
public int PropertyTypeID{get;set;}
[显示(Name=“属性类型”)]
[最大长度(250)]
[必需(ErrorMessage=“需要物业类型,即公寓、房屋、工作室”)]
公共字符串类型{get;set;}
[显示(Name=“Title”)]
[最大长度(250)]
[必需(ErrorMessage=“需要属性类型标题”)]
公共字符串标题{get;set;}
公共ICollection属性{get;set;}
公共ICollection PropertyRentingPrices{get;set;}
}

您是否连接到包含标识类的默认上下文,如果脚手架正在查看标识上下文中的每个类,您应该尝试为您的类创建一个新上下文,或者您可以将[key]放置在用户guid上方

public class RentingApplicationsViewModel
{
    public RentingApplicationsViewModel() {

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

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


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

}