You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core MVC+EF Core详情页无法显示配料问题排查

ASP.NET Core MVC + EF Core 详情页配料不显示问题分析

问题描述

基于ASP.NET Core MVC和EF Core的项目中,点击详情按钮欲展示配料,但页面空白;调试发现Recipe.RecipeIngredient计数为0,怀疑Details方法存在问题,相关实体类及方法代码如下:

实体类代码

public class Recipe
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    [ValidateNever]
    public string ImageUrl { get; set; }
    [ValidateNever]
    public ICollection<RecipeIngredient> RecipeIngredient { get;}
}

public class RecipeIngredient
{
    public int Id { get; set; }
    public int IngredientId { get; set; }
    [ValidateNever]
    public Ingredient Ingredient { get; set; }
    public int RecipeId { get; set; }
    [ValidateNever]
    public Recipe Recipe { get; set; }
}

public class Ingredient
{
    [Key]
    public int Id { get; set; }
    
    public string Name { get; set; }

    public int UnitOfMeasureId { get; set; }
    [ValidateNever]
    public UnitOfMeasure UnitOfMeasure { get; set; }
    
    public int QuantityOfMeasureId { get; set; }
    [ValidateNever]
    public QuantityOfMeasure QuantityOfMeasure { get; set; }
}

Details方法代码

public IActionResult Details(int? id)
{
        if (id == null || id == 0)
        {
            return NotFound();
        }

        var ingredients = _context.Recipes
                                    .Include(u => u.RecipeIngredient)
                                    .ThenInclude(e => e.Ingredient)
                                    .AsNoTracking()
                                    .FirstOrDefault(m => m.Id == id);

        if (ingredients == null)
        {
            return NotFound();
        }

        return View(ingredients);
}

问题排查与修正

Details方法的Include/ThenInclude加载关联数据的逻辑本身没有问题,问题出在实体类定义和EF Core关系配置上:

1. Recipe类集合未初始化

Recipe中的RecipeIngredient集合只有getter,未进行初始化,EF Core无法正确将数据库中的关联数据填充到该集合中。需修改为:

public class Recipe
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    [ValidateNever]
    public string ImageUrl { get; set; }
    [ValidateNever]
    public ICollection<RecipeIngredient> RecipeIngredient { get; } = new List<RecipeIngredient>();
}

2. EF Core多对多关系未正确配置

Recipe与Ingredient是多对多关系,通过RecipeIngredient作为连接实体,需在DbContext的OnModelCreating方法中明确配置关系:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // 推荐为连接实体配置复合主键(替代单独的Id字段,更符合多对多关系设计)
    modelBuilder.Entity<RecipeIngredient>()
        .HasKey(ri => new { ri.RecipeId, ri.IngredientId });

    // 配置Recipe与RecipeIngredient的一对多关系
    modelBuilder.Entity<RecipeIngredient>()
        .HasOne(ri => ri.Recipe)
        .WithMany(r => r.RecipeIngredient)
        .HasForeignKey(ri => ri.RecipeId)
        .OnDelete(DeleteBehavior.Cascade);

    // 配置Ingredient与RecipeIngredient的一对多关系
    modelBuilder.Entity<RecipeIngredient>()
        .HasOne(ri => ri.Ingredient)
        .WithMany() // 若Ingredient不需要反向导航集合,保留空的WithMany(); 若需要则添加对应的集合属性
        .HasForeignKey(ri => ri.IngredientId)
        .OnDelete(DeleteBehavior.Restrict);
}

3. 验证数据库关联数据

确认数据库中RecipeIngredient表存在对应RecipeId的记录,且IngredientId关联的Ingredient数据有效。

总结

修正实体类集合初始化问题,并正确配置EF Core的多对多关系后,Details方法就能正常加载配料数据,页面空白问题即可解决。

内容的提问来源于stack exchange,提问作者Marcus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:17:49