实体框架6通过外键选择

时间:2016-09-07 09:06:56

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

我的班级

public class User
{
    [Key]
    public int userId { get; set; }

    [Required(ErrorMessage = "Name Required.")]
    [Display(Name = "Name")]
    public string name { get; set; }
    [Required(ErrorMessage = "Username Required.")]
    [Display(Name = "Username")]
    public string username { get; set; }
    [Required(ErrorMessage = "Password Required.")]
    [Display(Name = "Password")]
    public string password { get; set; }
    [Required(ErrorMessage = "Phone Number Required.")]
    [Display(Name = "Phone Number")]
    public string phone { get; set; }
    [Display(Name = "Address")]
    public string address { get; set; }
    public ICollection<Service> services { get; set; }
}

public class Service
{
    [Key]
    public int serviceId { get; set; }

    [Display(Name = "Service Name")]
    [Required(ErrorMessage = "Service Name Required.")]
    public string serviceName { get; set; }
    [Display(Name = "Service Type")]
    [Required(ErrorMessage = "Select Service Type")]
    public int serviceType { get; set; }
    [Display(Name = "Service Price")]
    [Required(ErrorMessage = "Invalid Service Price")]
    public int servicePrice { get; set; }
}

以上两个类是我在代码第一种方法中的模型。我正在尝试查询属于特定用户的所有服务。

有人可以帮我吗? 提前谢谢

1 个答案:

答案 0 :(得分:1)

您需要提供服务&#39;在User类中将属性设置为虚拟。