1
votes

I would like to connect the database tables with EF Core 3.1, based on the code below. The problem is, that the ModellNavigation and the ManufacturerNavigation returns with null. What am I doing wrong? Please help me with this.

        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;
        }
3
Can you show your entity configuration for detail? - user3928241
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; } } - Peter Ruzsin
I think your configuration is lacking that is the reason why modelnavigation is null. Please check my answer. - user3928241
Your relationship configuration may be correct but you would still need to enable lazy loading or explicitly load via .Include - Aluan Haddad
Add that code to the question not as a comment - Aluan Haddad

3 Answers

0
votes

You need to define the relationship of Detail and ModelNavigation in your EntityFramework Configuration so that it will load model navigation when accessing the detail model. Please check this link: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key

0
votes

In this code segment the modell returns with null although it uses the lasy loading! Again, thanks for the help, I really appreciate it! (Please excuse my english, I'm not native!)

[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;
        }
0
votes

Enumerating like you do will make one query per loop, and if you also add lazy loading it will generate even more queries and complexity.

You should project your query directly in EF Core instead, which will generate only one query to the database.

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();
}