0

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

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 实体都为空。

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

4

2 回答 2

1

我认为你需要这样做利用InClude方法来获取子实体

context.Companies
             .Include("CompanyHours")
             .Where(e => e.Company.id == company.id) ;
于 2013-01-17T06:33:19.717 回答
0

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

于 2013-01-18T02:44:56.053 回答