【问题标题】:Querying the database using EF Code First and Linq使用 EF Code First 和 Linq 查询数据库
【发布时间】:2013-01-17 06:30:18
【问题描述】:

我花了很多时间,但不知道如何使这个查询工作。我正在制作一个小时的操作类型模块,用户可以在其中选择如下内容:

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 轻松映射动态生成的字段(这部分有效!!!:))

现在我想查询数据库并创建一个小时的操作模型以发送回视图(在获取时),以便我可以带回保存的信息。但是,我不知道该怎么做。我正在发回一个 HoursOfOperation 课程。

我目前的查询:

var model = company.CompanyHours.Where(e => e.Company.id == company.id);

只返回一个只有 id(正确的 id)的 HoursOfOperation 模型,所有 companyHours 实体都为空。

谁能帮我想出正确的查询?

【问题讨论】:

  • 您是否将延迟加载设置为 false?如果是这种情况,您必须自己加载它们
  • @websam 我删除了 virtual 关键字并在上下文中禁用了 LazyLoading 并且得到了相同的结果。

标签: c# linq entity-framework asp.net-mvc-4 ef-code-first


【解决方案1】:

我认为您需要使用InClude 方法来获取子实体

context.Companies
             .Include("CompanyHours")
             .Where(e => e.Company.id == company.id) ;

【讨论】:

  • 不幸的是,我得到了同样的结果
【解决方案2】:

好的,所以我终于解决了这个问题,但我这样做的方式是将我的模型完全重组为更好和正确的设计。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多