使用EF Code First和Linq查询数据库
本文关键字:Linq 查询 数据库 First EF Code 使用 | 更新日期: 2023-09-27 17:54:15
我花了很多时间,但不知道如何使这个查询工作。我正在制作一个小时的操作类型模块,用户可以选择以下内容:
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
}
为了完整,这里是我的companyInformation类
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; }
}
原因,我已经硬编码星期一,真的....在hoursofoperation类是为了使MVC更容易映射动态生成的字段(这部分工作!!):))
现在我想查询数据库并使一个小时的操作模型发送回视图(在get上),这样我就可以带回保存的信息。然而,我不知道如何做到这一点。我要返回一个HoursOfOperation类。
我当前的查询:
var model = company.CompanyHours.Where(e => e.Company.id == company.id);
只返回一个只有id(正确id)的HoursOfOperation模型,所有companyHours实体都为空。
谁能帮我想出合适的查询?
我认为您需要使用InClude
方法来获取子实体
context.Companies
.Include("CompanyHours")
.Where(e => e.Company.id == company.id) ;
好的,所以我最终解决了这个问题,但我做的方式是完全重构我的模型,以更好和正确的设计。