我花了很多时间,无法弄清楚如何使这个查询工作。我正在制作一个小时的操作类型模块,用户可以选择以下内容:
Monday open from 8am to 11am closed from 11am to 1pm and open from 1pm to 5pm
这是完全动态的,用户可以选择打开和关闭他们想要的数量(动态生成的表单输入)
为了做到这一点,我做了几个课程(我不知道这是否是一个正确的方法,因为我已经使用asp.net mvc,C#和EF大约一个星期了。任何建议都会非常感谢)
public class HoursOfOperation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public IQueryable<CompanyHour> Monday { get; set; }
public IQueryable<CompanyHour> Tuesday { get; set; }
public IQueryable<CompanyHour> Wednesday { get; set; }
public IQueryable<CompanyHour> Thursday { get; set; }
public IQueryable<CompanyHour> Friday { get; set; }
public IQueryable<CompanyHour> Saturday { get; set; }
public IQueryable<CompanyHour> Sunday { get; set; }
//Navigation Property
public CompanyInformation Company { get; set; }
}
和
public class CompanyHour
{
public int id { get; set; }
public Status Status { get; set; }
public string From { get; set; }
public string To { get; set; }
}
public enum Status
{
Closed,
Open,
ByAppointmentsOnly
}
完整性在这里是我的公司信息类
public class CompanyInformation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
//[Required]
[DisplayName("Company Name:")]
public string companyName { get; set; }
[DisplayName("Website Address:")]
[Url(ErrorMessage="The Website field is not a valid fully-qualified http, https, or ftp URL. (Example: http://www.website.com)")]
public string website { get; set; }
public string contactTitle { get; set; }
//[Required]
[DisplayName("Contact First Name:")]
public string contactFirstName { get; set; }
//[Required]
[DisplayName("Contact Last Name:")]
public string contactLastName { get; set; }
//[Required]
[Phone]
[DisplayName("Phone Number:")]
public string contactPhoneNumber { get; set; }
[DisplayName("Address Display?")]
public bool displayAddress { get; set; }
[DisplayName("Phone Number?")]
public bool displayPhoneNumber { get; set; }
//[Required]
[DisplayName("Address 1:")]
public string address1 { get; set; }
[DisplayName("Address 2:")]
public string address2 { get; set; }
//[Required]
[DisplayName("City:")]
public string city { get; set; }
//[Required]
[DisplayName("State:")]
public string state { get; set; }
//[Required]
[DisplayName("Zip/Postal Code:")]
public string zipCode { get; set; }
[DisplayName("Search Engine?")]
public bool allowSearchEngines { get; set; }
//navigation Properties
public virtual ICollection<UserProfile> CompanyUsers { get; set; }
public virtual ICollection<HoursOfOperation> CompanyHours { get; set; }
}
原因,我已经硬编码了星期一,星期二......在小时操作课程中是为了让mvc轻松映射动态生成的字段(而那部分工作!!! :))
现在我想查询数据库并制作一个小时的操作模型以发送回视图(在获取时),这样我就可以恢复保存的信息。然而,我无法弄清楚如何做到这一点。我正在寄回一个HoursOfOperation课程。
我目前的查询:
var model = company.CompanyHours.Where(e => e.Company.id == company.id);
只返回一个只有id(正确的id)的HoursOfOperation模型,所有的companyHours实体都是null。
任何人都可以帮我提出正确的查询吗?
答案 0 :(得分:1)
我认为你需要这样做才能利用InClude
方法来获得孩子的恩惠
context.Companies
.Include("CompanyHours")
.Where(e => e.Company.id == company.id) ;
答案 1 :(得分:0)
好的,所以我终于解决了这个问题,但我这样做的方式是将我的模型完全重构为更好更正确的设计。