【问题标题】:EF Core 3.1 Navigation propertyEF Core 3.1 导航属性
【发布时间】:2020-09-18 18:18:28
【问题描述】:

我想根据下面的代码将数据库表与 EF Core 3.1 连接起来。 问题是,ModellNavigation 和 ManufacturerNavigation 返回 null。 我究竟做错了什么?请帮我解决这个问题。

        public IEnumerable<ViewModel> GetAll()
        {
            List<ViewModel> models = new List<ViewModel>();
            foreach(Detail detail in _context.Detail)
            {
                ViewModel viewModel = new ViewModel
                {
                    ID = detail.DetailId,
                    Manufacturer = detail.ModellNavigation.ManufacturerNavigation.Name,
                    Modell = detail.ModellNavigation.Name,
                    Consumption = detail.Consumption,
                    Color = detail.Color,
                    Year = detail.Year,
                    Horsepower = detail.Horsepower
                };
                models.Add(viewModel);
            }
            return models;
        }

【问题讨论】:

  • 能否详细说明您的实体配置?
  • public partial class Detail { public int DetailId { get; set; } public decimal Consumption { get; set; } public string Color { get; set; } public DateTime Year { get; set; } public int Horsepower { get; set; } public int Modell { get; set; } public virtual Modell ModellNavigation { get; set; } }
  • 我认为您的配置不足,这就是 modelnavigation 为空的原因。请检查我的答案。
  • 您的关系配置可能正确,但您仍需要启用延迟加载或通过.Include显式加载
  • 将该代码添加到问题中,而不是作为评论

标签: .net-core entity-framework-core


【解决方案1】:

您需要在 EntityFramework Configuration 中定义 Detail 和 ModelNavigation 的关系,以便在访问详细模型时加载模型导航。 请检查此链接: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key

【讨论】:

  • 对不起。概念是一样的。但我更新了链接。
【解决方案2】:

在此代码段中,模型返回 null,尽管它使用了延迟加载! 再次感谢您的帮助,我真的很感激! (请原谅我的英语,我不是本地人!)

[HttpGet]
        public IEnumerable<CarView> getAllCars()
        {
            List<CarView> carViews = new List<CarView>();
            foreach(Manufacturer manufacturer in _context.Manufacturer)
            {
                CarView car = new CarView();
                car.Manufacturer = manufacturer.Name;
                foreach(Modell modell in manufacturer.Modell)
                {
                    car.Modell = modell.Name;
                    foreach(Detail detail in modell.Details)
                    {
                        car.ID = detail.DetailId;
                        car.Consumption = detail.Consumption;
                        car.Color = detail.Color;
                        car.Year = detail.Year;
                        car.Horsepower = detail.Horsepower;
                    }
                }
                carViews.Add(car);
            }
            return carViews;
        }

【讨论】:

    【解决方案3】:

    像你这样枚举会在每个循环中产生一个查询,如果你还添加延迟加载,它会产生更多的查询和复杂性。

    您应该直接在 EF Core 中投影您的查询​​,这将只生成一个对数据库的查询。

    public IEnumerable<ViewModel> GetAll()
    {
        return _context.Detail.Select(
            d => new ViewModel()
            {
                ID = detail.DetailId,
                Manufacturer = detail.ModellNavigation.ManufacturerNavigation.Name,
                Modell = detail.ModellNavigation.Name,
                Consumption = detail.Consumption,
                Color = detail.Color,
                Year = detail.Year,
                Horsepower = detail.Horsepower
            }
        ).ToList();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 2018-10-10
      • 2022-01-04
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多