【问题标题】:asp.net mvc viewmodel entity has no key Error when trying to create view from controller尝试从控制器创建视图时,asp.net mvc viewmodel 实体没有键错误
【发布时间】:2015-08-07 11:37:10
【问题描述】:

我在 ASP.NET-MVC 应用程序中有 ViewModel 类,当我尝试创建视图时 --> 来详细说明记录。我收到实体没有主键的错误。我有测试个人模型类,它们工作正常,我不是我在这里缺少的......

ViewModel 类

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

}

【问题讨论】:

    标签: asp.net-mvc-5 entity-framework-6 asp.net-mvc-viewmodel


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多