EF Core w/ Sql Server one-to-many relationship not working

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


I'm trying to create a simple RESTful API that can retrieve information from a Sql Server DB. I'm using .Net Core, together with EF Core for ORM.

I have mapped two tables in my database by foreign key. However, when i try to implement the relationship in EF core, it seems that no rows exist in the BaseStats table that reference the given champion row in the Champions table.

I get the following result when making the API call:

"championId": 1,
"championName": "Vladimir",
"baseStats": null

It seems that EF core somehow doesn't recognize the relationship. I must have missed something.

Any advice would be greatly appreciated :) Below is my code. Thanks in advance.

Principal entity:

public class Champion
    public int ChampionId                  { get; set; }
    public string ChampionName             { get; set; }

    public ICollection<BaseStat> BaseStats { get; set; }

Dependent entity

public class BaseStat
    public int      BaseStatId { get; set; }
    public int      StatTypeId { get; set; }
    public decimal  Base       { get; set; }
    public decimal  Growth     { get; set; }

    public Champion Champion   { get; set; }

DB context

public class Context : DbContext
    public DbSet<Champion>  Champions { get; set; }
    public DbSet<BaseStat>  BaseStats { get; set; }

    public Context(DbContextOptions<Context> options) : base (options)

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            .HasOne(p => p.Champion)
            .WithMany(b => b.BaseStats);



public class ChampionController : Controller

    private readonly Context _context;

    public ChampionController(Context context)
        _context = context; 

    public async Task<JsonResult> Stats()
        var champ = await _context.Champions.FirstOrDefaultAsync(c => c.ChampionId == 1);
        return Json(champ);

7/22/2017 3:24:10 PM

Popular Answer

I updated the stats action in my controller according to Ivan Stoev's answer.

public async Task<JsonResult> Stats()
Champion champion = await _context.Champions
    .Include(c => c.BaseStats)
    return Json(champion);

While the action returns the correct information, when i make the API call through the browser, it seems to keep loading as if the call doesn't end. (spinner in browser tab, google chrome)

While at the same time, i can't make the API call in Postman without getting the following error:

Could not get any response
There was an error connecting to http://localhost:50185/api/champion/stats.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Client certificates are required for this server:
Fix this by adding client certificates in Settings > Certificates
Request timeout:
Change request timeout in Settings > General

Does anyone have an idea as to what might cause this?

7/22/2017 4:23:48 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