EF7 beta5: Foreign Key returns null value

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


I have created an API using ASP.NET5 and Entity Framework 7.0.0-beta 5.

I have the Model, DbContext and the Repository created, and when I try to retrieve data from my database I get all the data, except the foreign key data.

The foreign key value is always null.


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>().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>().Key(c => c.StatusId);




public class Company

    public int CompanyId { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public CompanyStatus Status { get; set; }

    public DateTime CreatedAt { get; set; }

    public DateTime UpdatedAt { get; set; }


Repository (only get all method)

    public IEnumerable<Company> GetAll()
        return _dbcontext.Company.ToList();

Retrieving data using HTTP GET I get all the data, but the value of the foreign key is null:

JSON Response:

0:  {
CompanyId: 1
Name: "Hotel Amura"
Email: "info@hotelamura.com"
Status: null
CreatedAt: "2015-01-01T00:00:00"
UpdatedAt: "2015-01-01T00:00:00"

Does anyone know how to get the Foreign Key table data using EF7 query?

7/6/2015 9:41:03 PM

Accepted Answer

Lazy loading isn't implemented yet in EF7. Use the following query to eagerly-load the related entities instead.

_dbcontext.Company.Include(c => c.Status).ToList();
7/6/2015 10:42:26 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow