C# EF Core与Sql Server一对多关系不起作用

C# EF Core与Sql Server一对多关系不起作用,c#,sql-server,asp.net-core,entity-framework-core,C#,Sql Server,Asp.net Core,Entity Framework Core,我正在尝试创建一个简单的RESTful API,它可以从SQLServerDB检索信息。我正在使用.NETCore,以及用于ORM的EFCore 我用外键映射了数据库中的两个表。但是,当我尝试在EF core中实现该关系时,BaseStats表中似乎不存在引用冠军表中给定冠军行的行 我在进行API调用时得到以下结果: { "championId": 1, "championName": "Vladimir", "baseStats": null } EF core似乎不知何故没有认识到这种关

我正在尝试创建一个简单的RESTful API,它可以从SQLServerDB检索信息。我正在使用.NETCore,以及用于ORM的EFCore

我用外键映射了数据库中的两个表。但是,当我尝试在EF core中实现该关系时,BaseStats表中似乎不存在引用冠军表中给定冠军行的行

我在进行API调用时得到以下结果:

{
"championId": 1,
"championName": "Vladimir",
"baseStats": null
 }
EF core似乎不知何故没有认识到这种关系。我一定错过了什么

任何建议都将不胜感激:)以下是我的代码。提前谢谢

主要实体:

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

    public ICollection<BaseStat> BaseStats { get; set; }
}
DB上下文

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)
    {
        modelBuilder.Entity<BaseStat>()
            .HasOne(p => p.Champion)
            .WithMany(b => b.BaseStats);
    }

}
公共类上下文:DbContext
{
公共数据库集{get;set;}
公共DbSet BaseStats{get;set;}
公共上下文(DbContextOptions):基本(选项)
{
//
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(p=>p.Champion)
.具有多个(b=>b.BaseStats);
}
}
控制器

[Route("api/[controller]/[action]")]
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);
    }

}
[路由(“api/[控制器]/[操作]”)
公共类ChampionController:控制器
{
私有只读上下文\u上下文;
公共ChampionController(上下文)
{
_上下文=上下文;
}
公共异步任务Stats()
{
var champ=wait_context.champs.FirstOrDefaultAsync(c=>c.ChampionId==1);
返回Json(champ);
}
}

我根据伊万·斯托夫的回答更新了控制器中的统计操作

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

有人知道这可能是什么原因吗?

我会尝试使用一个具体的列表而不是ICollection,然后重新生成迁移。关系正常,但EF Core中没有自动(延迟)加载。看见很快,您就需要像
\u context.Champions.Include(c=>c.BaseStats).FirstOrDefault(…)
这样的东西了。谢谢!基本上,当您加载关系的两侧时,EF Core将修复导航属性,这将导致JsonSerializer中的循环和无限递归。您需要在序列化程序中将
ReferenceLoopHandling
设置为忽略。我的回答晚了,但我只想指出,这个回答解决了我的问题。谢谢你,斯密特
public async Task<JsonResult> Stats()
{
Champion champion = await _context.Champions
    .Include(c => c.BaseStats)
    .FirstOrDefaultAsync();
    return Json(champion);
}
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