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.
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);
}
}
Model
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?
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.