繁体   English   中英

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

[英]EF 6: Include not building navigation properties

我似乎无法弄清楚为什么我的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 }));

谁能看到我在这里缺少什么?

可能与在AccountStartel构造函数中设置Company和/或CompanyId有关吗? 如果删除这些行,是否可行? –彼得

将导航属性初始化为默认值会导致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>();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM