EF 6:包括不构建导航属性

时间:2015-09-29 20:22:41

标签: c# entity-framework

我似乎无法弄清楚为什么导航属性不是由我的include语句构建的。

这是我的方法:

public async Task<IHttpActionResult> GetCompanies(string id)
    {
        DbContext.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        var company = await DbContext.Companies.Where(x => x.Id.ToString() == id).Include(x => x.StartelAccounts).FirstOrDefaultAsync();

        if (company != null) 
        {
            return Ok(this.TheModelFactory.Create(company));
        }

        return NotFound();
    }

当我从调试日志中测试SQL时,我得到了两个对象的所有字段和值。

以下是模型:

public class CompanyGroup 
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [MaxLength(100)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Date)]
    public DateTime FirstBillingDate { get; set; }

    [Required]
    public int TermLength { get; set; }

    public virtual ICollection<ApplicationUser> Members { get; set; }
    public virtual ICollection<AccountStartel> StartelAccounts { get; set; }

    public CompanyGroup() 
    {
        Members = new HashSet<ApplicationUser>();
        StartelAccounts = new HashSet<AccountStartel>();
    }

}
public class AccountStartel
{

    [Key]
    public Guid Id { get; set; }

    [Required]
    public string ClientID { get; set; }

    [Required]
    public int DbId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string TimeZone { get; set; }

    [Required]
    public string AccountNum { get; set; }

    public Guid CompanyId { get; set; }

    public virtual CompanyGroup Company { get; set; }

    public virtual ICollection<UsageReport> UsageReports { get; set; }

    public AccountStartel() 
    {
        Company = new CompanyGroup();
        CompanyId = Guid.Empty;
        UsageReports = new List<UsageReport>();
    }
}

EF Fluent API

modelBuilder.Entity<AccountStartel>()
            .HasRequired<CompanyGroup>(x => x.Company)
            .WithMany(x => x.StartelAccounts)
            .HasForeignKey(x => x.CompanyId);

        modelBuilder.Entity<AccountStartel>()
            .Property(p => p.DbId)
            .IsRequired()
            .HasColumnAnnotation(
                IndexAnnotation.AnnotationName,
                new IndexAnnotation(
                    new System.ComponentModel.DataAnnotations.Schema.IndexAttribute("IX_StartelDbId", 1) { IsUnique = true }));

谁能看到我在这里失踪的东西?

1 个答案:

答案 0 :(得分:1)

  

是否与设置公司和/或CompanyId有关   AccountStartel构造函数?如果删除这些行,它会起作用吗? -   彼得

将导航属性初始化为默认值会导致EF无法正确加载它们。

这是现在可以使用的更新模型

public class AccountStartel
{

    [Key]
    public Guid Id { get; set; }

    [Required]
    public string ClientID { get; set; }

    [Required]
    public int DbId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string TimeZone { get; set; }

    [Required]
    public string AccountNum { get; set; }

    public Guid CompanyId { get; set; }

    public CompanyGroup Company { get; set; }

    public virtual ICollection<UsageReport> UsageReports { get; set; }

    public AccountStartel() 
    {
        UsageReports = new List<UsageReport>();
    }
}