asp.net mvc viewmodel entity has no key 尝试从控制器创建视图时出错
asp.net mvc viewmodel entity has no key Error when trying to create view from controller
我在 ASP.NET-MVC 应用程序中有 ViewModel class,当我尝试创建视图时 --> 详细记录。我收到实体没有主键的错误。我已经测试了个人模型 classes 并且它们工作正常,我不是我在这里缺少的...
视图模型class
public class RentingApplicationsViewModel
{
public RentingApplicationsViewModel() {
_PropertyRentingApplicationModel = new PropertyRentingApplication();
_PropertyTypeModel = new PropertyType();
}
public PropertyRentingApplication _PropertyRentingApplicationModel { get; set; }
public PropertyType _PropertyTypeModel { get; set; }
}
型号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; }
}
您是否连接到包含身份 classes 的默认上下文,如果是这样,脚手架正在查看身份上下文中的每个 class,您应该尝试为您的class 或者您可以将 [key] 放在用户 guid 上方。
我在 ASP.NET-MVC 应用程序中有 ViewModel class,当我尝试创建视图时 --> 详细记录。我收到实体没有主键的错误。我已经测试了个人模型 classes 并且它们工作正常,我不是我在这里缺少的...
视图模型class
public class RentingApplicationsViewModel
{
public RentingApplicationsViewModel() {
_PropertyRentingApplicationModel = new PropertyRentingApplication();
_PropertyTypeModel = new PropertyType();
}
public PropertyRentingApplication _PropertyRentingApplicationModel { get; set; }
public PropertyType _PropertyTypeModel { get; set; }
}
型号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; }
}
您是否连接到包含身份 classes 的默认上下文,如果是这样,脚手架正在查看身份上下文中的每个 class,您应该尝试为您的class 或者您可以将 [key] 放在用户 guid 上方。