EF7 beta5:外鍵返回空值

asp.net-core c# entity-framework-core

我使用ASP.NET5和Entity Framework 7.0.0-beta 5創建了一個API。

我創建了Model,DbContext和Repository,當我嘗試從數據庫中檢索數據時,除了外鍵數據外,我得到了所有數據。

外鍵值始終為null。

的DbContext

public class MrBellhopContext : DbContext
{

    public DbSet<Company> Company { get; set; }
    public DbSet<CompanyStatus> CompanyStatus { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Company>().Table("Company");
        modelBuilder.Entity<Company>().Key(c => c.CompanyId);
        modelBuilder.Entity<Company>().Index(c => c.Name);
        modelBuilder.Entity<Company>().Reference(c => c.Status).InverseReference().ForeignKey<CompanyStatus>(c => c.StatusId);

        modelBuilder.Entity<CompanyStatus>().Table("CompanyStatus");
        modelBuilder.Entity<CompanyStatus>().Key(c => c.StatusId);

        base.OnModelCreating(modelBuilder);
    }

}

模型

public class MrBellhopContext : DbContext
{

    public DbSet<Company> Company { get; set; }
    public DbSet<CompanyStatus> CompanyStatus { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Company>().Table("Company");
        modelBuilder.Entity<Company>().Key(c => c.CompanyId);
        modelBuilder.Entity<Company>().Index(c => c.Name);
        modelBuilder.Entity<Company>().Reference(c => c.Status).InverseReference().ForeignKey<CompanyStatus>(c => c.StatusId);

        modelBuilder.Entity<CompanyStatus>().Table("CompanyStatus");
        modelBuilder.Entity<CompanyStatus>().Key(c => c.StatusId);

        base.OnModelCreating(modelBuilder);
    }

}

存儲庫 (只獲取所有方法)

public class MrBellhopContext : DbContext
{

    public DbSet<Company> Company { get; set; }
    public DbSet<CompanyStatus> CompanyStatus { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Company>().Table("Company");
        modelBuilder.Entity<Company>().Key(c => c.CompanyId);
        modelBuilder.Entity<Company>().Index(c => c.Name);
        modelBuilder.Entity<Company>().Reference(c => c.Status).InverseReference().ForeignKey<CompanyStatus>(c => c.StatusId);

        modelBuilder.Entity<CompanyStatus>().Table("CompanyStatus");
        modelBuilder.Entity<CompanyStatus>().Key(c => c.StatusId);

        base.OnModelCreating(modelBuilder);
    }

}

使用HTTP GET檢索數據我獲取所有數據,但外鍵的值為null:

JSON響應:

public class MrBellhopContext : DbContext
{

    public DbSet<Company> Company { get; set; }
    public DbSet<CompanyStatus> CompanyStatus { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Company>().Table("Company");
        modelBuilder.Entity<Company>().Key(c => c.CompanyId);
        modelBuilder.Entity<Company>().Index(c => c.Name);
        modelBuilder.Entity<Company>().Reference(c => c.Status).InverseReference().ForeignKey<CompanyStatus>(c => c.StatusId);

        modelBuilder.Entity<CompanyStatus>().Table("CompanyStatus");
        modelBuilder.Entity<CompanyStatus>().Key(c => c.StatusId);

        base.OnModelCreating(modelBuilder);
    }

}

有誰知道如何使用EF7查詢獲取外鍵表數據?

一般承認的答案

EF7中尚未實現延遲加載。使用以下查詢來急切加載相關實體。

_dbcontext.Company.Include(c => c.Status).ToList();



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因