locked
Foreign Key properly set but can't access the data through foreign key. RRS feed

  • Question

  • User1153554953 posted

    Hello Guys, Hope everyone is doing well. First, I would like to thank everyone for contributing to this forum, I am new to ASP.NET Core and here people helped me a lot to solve my problems, I was having a problem to set foreign key in my database and I posted the issue here which got solved but it lead me to a new problem so I decided to make a separate thread for it in a relevant category.

    Okay so, I have three classes, Dinghy, Manufacturer and ClassAssociation, each Dinghy have 1 manufacturer and 1 class association and so each table of Dinghy includes its respective foreign keys for manufacturer and class association. The problem is that, when I add Dingies to database through a form, everything works well and table of Dinghies receives the IDs of Manufacturer and Class Association, but when I proceed to display the name of Manufacturer and Class Association using the foreign keys, it shows null. Debugging has shown me that the object of Manufacturer and Class Association is storing null. Why is it so when I have properly configured the foreign keys?

    I am attaching the relevant code for you guys to dig out the issue.

    .CSHTML File (I am using this syntax to display the data) 

                <td>
                    @Html.DisplayFor(modelItem => item.Manufacturers.Manufacturer)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CA.CAName)
                </td>

    ListDinghies Action in Controller

    public async Task<IActionResult> ListDinghies()
            {
                return View(await _context2.Dinghies.ToListAsync());
            }

    AddDinghies Post Action in Controller

     public async Task<IActionResult> AddDinghies([Bind("id,Name,OlympicBoat,Type,TypeCrew,TypePurpose,Length,Beam,Draft,SailAreaUpwind,GennakerSailArea,SpinnakerSailArea,SAOptional,SAOptional2,Weight,PYS,ImagePath,CrewPersons,CrewWeight,Description,DesignBy,DesignYear,OlympicSectionViewSwitch,ManufacturerID,ClassAssociationID")] Dinghies dg)
            {
                if (ModelState.IsValid)
                {
                   //_context2.Manufacturers.Include(e => e.Manufacturer).FirstOrDefault(m => m.id == );
                    _context2.Add(dg);
                    await _context2.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                return View(dg);
            }

    Dinghies.cs (Only showing section where foreign keys and navigation properties are added)

            [ForeignKey("Manufacturers")]
            public int ManufacturerID { get; set; }
            public Manufacturers Manufacturers { get; set; }
    
            [ForeignKey("CA")]
            public int ClassAssociationID { get; set; }
            public CA CA { get; set; }
    

    I have searched a lot on internet but couldn't find the solution, I believe only an expert can figure out the problem. Thanks

    Saturday, August 29, 2020 5:53 AM

Answers

  • User303363814 posted

    return View(await _context2.Dinghies.ToListAsync());

    In ListDinghies you need to .Include (eager load) the dependent tables

    return View (await _context2.Dinghies.Include(d => d.Manufacturers).Include(d => d.CA).ToListAsync();

    The field called Manufacturers has a plural name but seems to refer to a single thing, that's a bit confusing.  Normally there would be a class in the Model called Manufacturer (singular because each instance represents one thing) which is stored in a table called Manufacturers (plural because it contains many of them).  Does a Dinghy have one Manufacturer or many Manufacturers?  (Names are important!  They help us think about the things which are named)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 30, 2020 6:18 AM

All replies

  • User1153554953 posted

    Hello Guys, Hope everyone is doing well. First, I would like to thank everyone for contributing to this forum, I am new to ASP.NET Core and here people helped me a lot to solve my problems, I was having a problem to set foreign key in my database and I posted the issue here which got solved but it lead me to a new problem so I decided to make a separate thread for it in a relevant category.

    Okay so, I have three classes, Dinghy, Manufacturer and ClassAssociation, each Dinghy have 1 manufacturer and 1 class association and so each table of Dinghy includes its respective foreign keys for manufacturer and class association. The problem is that, when I add Dingies to database through a form, everything works well and table of Dinghies receives the IDs of Manufacturer and Class Association, but when I proceed to display the name of Manufacturer and Class Association using the foreign keys, it shows null. Debugging has shown me that the object of Manufacturer and Class Association is storing null. Why is it so when I have properly configured the foreign keys?

    I am attaching the relevant code for you guys to dig out the issue.

    .CSHTML File (I am using this syntax to display the data) 

                <td>
                    @Html.DisplayFor(modelItem => item.Manufacturers.Manufacturer)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CA.CAName)
                </td>

    ListDinghies Action in Controller

    public async Task<IActionResult> ListDinghies()
            {
                return View(await _context2.Dinghies.ToListAsync());
            }

    AddDinghies Post Action in Controller

     public async Task<IActionResult> AddDinghies([Bind("id,Name,OlympicBoat,Type,TypeCrew,TypePurpose,Length,Beam,Draft,SailAreaUpwind,GennakerSailArea,SpinnakerSailArea,SAOptional,SAOptional2,Weight,PYS,ImagePath,CrewPersons,CrewWeight,Description,DesignBy,DesignYear,OlympicSectionViewSwitch,ManufacturerID,ClassAssociationID")] Dinghies dg)
            {
                if (ModelState.IsValid)
                {
                   //_context2.Manufacturers.Include(e => e.Manufacturer).FirstOrDefault(m => m.id == );
                    _context2.Add(dg);
                    await _context2.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                return View(dg);
            }

    Dinghies.cs (Only showing section where foreign keys and navigation properties are added)

            [ForeignKey("Manufacturers")]
            public int ManufacturerID { get; set; }
            public Manufacturers Manufacturers { get; set; }
    
            [ForeignKey("CA")]
            public int ClassAssociationID { get; set; }
            public CA CA { get; set; }

    I have searched a lot on internet but couldn't find the solution, I believe only an expert can figure out the problem. Thanks

    Saturday, August 29, 2020 12:10 PM
  • User1153554953 posted

    Hello Guys, Hope everyone is doing well. First, I would like to thank everyone for contributing to this forum, I am new to ASP.NET Core and here people helped me a lot to solve my problems, I was having a problem to set foreign key in my database and I posted the issue here which got solved but it lead me to a new problem so I decided to make a separate thread for it in a relevant category.

    Okay so, I have three classes, Dinghy, Manufacturer and ClassAssociation, each Dinghy have 1 manufacturer and 1 class association and so each table of Dinghy includes its respective foreign keys for manufacturer and class association. The problem is that, when I add Dingies to database through a form, everything works well and table of Dinghies receives the IDs of Manufacturer and Class Association, but when I proceed to display the name of Manufacturer and Class Association using the foreign keys, it shows null. Debugging has sh

    Anybody?

    Sunday, August 30, 2020 12:53 AM
  • User303363814 posted

    Where is the code that retrieves data?  Do you have the appropriate .Include() clause so that the related tables are fetched?

    Sunday, August 30, 2020 3:17 AM
  • User1153554953 posted

    Where is the code that retrieves data?  Do you have the appropriate .Include() clause so that the related tables are fetched?

    Oh thank God, at least somebody replied after 24 hours.

    Thank you Paul, I have already included the code that receives the request

    AddDinghies Post Action in Controller

     public async Task<IActionResult> AddDinghies([Bind("id,Name,OlympicBoat,Type,TypeCrew,TypePurpose,Length,Beam,Draft,SailAreaUpwind,GennakerSailArea,SpinnakerSailArea,SAOptional,SAOptional2,Weight,PYS,ImagePath,CrewPersons,CrewWeight,Description,DesignBy,DesignYear,OlympicSectionViewSwitch,ManufacturerID,ClassAssociationID")] Dinghies dg)
            {
                if (ModelState.IsValid)
                {
                   //_context2.Manufacturers.Include(e => e.Manufacturer).FirstOrDefault(m => m.id == );
                    _context2.Add(dg);
                    await _context2.SaveChangesAsync();
                    return RedirectToAction(nameof(Index));
                }
                return View(dg);
            }

    I have not included any include tag in the code and I don't know about that too, I am dealing with foreign keys for the first time so I suppose that system will do the job at the backend once the table gets the foreign key ID, am I wrong? What change do I need to bring in the code?

    Sunday, August 30, 2020 5:28 AM
  • User303363814 posted

    That code saves the data.  Where is the code that retrieves the data. (Probably in your Index action)

    Sunday, August 30, 2020 5:31 AM
  • User1153554953 posted

    That code saves the data.  Where is the code that retrieves the data. (Probably in your Index action)

    This is where the whole process begins, AddDinghies Action:

     public IActionResult AddDinghies() {
                var Manufacturers = _context3.Manufacturers.ToList();
                var ClassAssociations = _context.CA.ToList();
                List<SelectListItem> ManList = new List<SelectListItem>();
                List<SelectListItem> CAList = new List<SelectListItem>();
                foreach (var manufacturer in Manufacturers)
                {
                    ManList.Add(new SelectListItem
                    {
                        Value = manufacturer.id.ToString(),
                        Text = manufacturer.Manufacturer //Name of manufacturer
                    });
                }
                foreach (var classassociation in ClassAssociations)
                {
                    CAList.Add(new SelectListItem
                    {
                        Value = classassociation.CAid.ToString(),
                        Text = classassociation.CAName 
                    });
                }

    The view against this action is:

    AddDinghies.cshtml

    @model Dinghysailing.info.Models.Dinghies
    
    @{
        Layout = "~/Views/Shared/AdminLayout.cshtml";
    }
    
    @*<!DOCTYPE html>
    
        <html>
        <head>
            <meta name="viewport" content="width=device-width" />
            <title>AddDinghies</title>
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        </head>
        <body>*@
    
    <h4>Dinghies</h4>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form asp-action="AddDinghies">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input asp-for="Name" class="form-control" />
                    <span asp-validation-for="Name" class="text-danger"></span>
                </div>
                @*<div class="form-group">
                <label asp-for="Manufacturer" class="control-label"></label>
                <input asp-for="Manufacturer" class="form-control" />
                <span asp-validation-for="Manufacturer" class="text-danger"></span>
            </div>*@
    
                <div class="form-group">
                    <label asp-for="ManufacturerID" class="control-label">Manufacturer</label>
                    <select class="form-control" asp-for="ManufacturerID" asp-items="ViewBag.Manufacturers"></select>
                    <span asp-validation-for="ManufacturerID"></span>
                </div>
                <div class="form-group">
                    <label asp-for="ClassAssociationID" class="control-label"> Class Association</label>
                    <select class="form-control" asp-for="ClassAssociationID" asp-items="ViewBag.ClassAssociations"></select>
                    <span asp-validation-for="ClassAssociationID"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Type" class="control-label"></label>
                    @*<input asp-for="Type" class="form-control" />*@
                    <select asp-for="Type" class="form-control">
                        <option value="Skiff">Skiff</option>
                        <option value="Catamaran">Catamaran</option>
                        <option value="Trimaran">Trimaran</option>
                        <option value="Monohull">Monohull</option>
                        <option value="Foiling">Foiling</option>
                        <option value="Adventurers">Adventurers</option>
                        <option value="Others">Others</option>
                    </select>
                    <span asp-validation-for="Type" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="OlympicSectionViewSwitch" class="control-label"></label>
                    <select asp-for="OlympicSectionViewSwitch" class="form-control">
                        <option value="Yes">Yes</option>
                        <option value="No">No</option>
                    </select>
                    <span asp-validation-for="OlympicSectionViewSwitch" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="OlympicBoat" class="control-label"></label>
                    <select asp-for="OlympicBoat" class="form-control">
                        <option value="Yes">Yes</option>
                        <option value="No">No</option>
                        <option value="Formerly">Formerly</option>
                    </select>
                    <span asp-validation-for="OlympicBoat" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="TypeCrew" class="control-label"></label>
                    @*<input asp-for="TypeCrew" class="form-control" />*@
                    <select asp-for="TypeCrew" class="form-control">
                        <option value="Singlehanded">Singlehanded</option>
                        <option value="Doublehanded">Doublehanded</option>
                        <option value="Multihanded">Multihanded</option>
                        <option value="Various">Various</option>
                    </select>
                    <span asp-validation-for="TypeCrew" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="TypePurpose" class="control-label"></label>
                    @*<input asp-for="TypePurpose" class="form-control" />*@
                    <select asp-for="TypePurpose" class="form-control">
                        <option value="Racing">Racing</option>
                        <option value="Education">Education</option>
                    </select>
                    <span asp-validation-for="TypePurpose" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Length" class="control-label"></label>
                    <input asp-for="Length" class="form-control" />
                    <span asp-validation-for="Length" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Beam" class="control-label"></label>
                    <input asp-for="Beam" class="form-control" />
                    <span asp-validation-for="Beam" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Draft" class="control-label"></label>
                    <input asp-for="Draft" class="form-control" />
                    <span asp-validation-for="Draft" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SailAreaUpwind" class="control-label"></label>
                    <input asp-for="SailAreaUpwind" class="form-control" />
                    <span asp-validation-for="SailAreaUpwind" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="GennakerSailArea" class="control-label"></label>
                    <input asp-for="GennakerSailArea" class="form-control" />
                    <span asp-validation-for="GennakerSailArea" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SpinnakerSailArea" class="control-label"></label>
                    <input asp-for="SpinnakerSailArea" class="form-control" />
                    <span asp-validation-for="SpinnakerSailArea" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SAOptional" class="control-label"></label>
                    <input asp-for="SAOptional" class="form-control" />
                    <span asp-validation-for="SAOptional" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="SAOptional2" class="control-label"></label>
                    <input asp-for="SAOptional2" class="form-control" />
                    <span asp-validation-for="SAOptional2" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Weight" class="control-label"></label>
                    <input asp-for="Weight" class="form-control" />
                    <span asp-validation-for="Weight" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="PYS" class="control-label"></label>
                    <input asp-for="PYS" class="form-control" />
                    <span asp-validation-for="PYS" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="CrewPersons" class="control-label"></label>
                    <input asp-for="CrewPersons" class="form-control" />
                    <span asp-validation-for="CrewPersons" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="CrewWeight" class="control-label"></label>
                    <input asp-for="CrewWeight" class="form-control" />
                    <span asp-validation-for="CrewWeight" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DesignBy" class="control-label"></label>
                    <input asp-for="DesignBy" class="form-control" />
                    <span asp-validation-for="DesignBy" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="DesignYear" class="control-label"></label>
                    <input asp-for="DesignYear" class="form-control" />
                    <span asp-validation-for="DesignYear" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="ImagePath" class="control-label"></label>
                    <input asp-for="ImagePath" class="form-control" />
                    <span asp-validation-for="ImagePath" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Create" class="btn btn-primary" />
                </div>
            </form>
        </div>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    
    

    The form when submitted goes to the AddDinghies action I mentioned in previous post. Now lets come to retrieving the data from database.

    This action is used to retrieve the data

    ListDinghies Action

    public async Task<IActionResult> ListDinghies()
            {
                return View(await _context2.Dinghies.ToListAsync());
            }

    and the view against this action is this, 

    ListDinghies.cshtml

    @model IEnumerable<Dinghysailing.info.Models.Dinghies>
    
    @{
        Layout = "~/Views/Shared/AdminLayout.cshtml";
    }
    
    @*<!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>ListDinghies</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    </head>
    <body>*@
    <p>
        <a asp-action="AddDinghies">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Type)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.TypeCrew)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.TypePurpose)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Length)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Beam)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Draft)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SailAreaUpwind)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.GennakerSailArea)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SpinnakerSailArea)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SAOptional)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.SAOptional2)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Weight)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.PYS)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ImagePath)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Manufacturers.Manufacturer)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CA.CAName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CrewPersons)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CrewWeight)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.DesignBy)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.DesignYear)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.OlympicBoat)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.OlympicSectionViewSwitch)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Description)
                </th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Type)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.TypeCrew)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.TypePurpose)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Length)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Beam)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Draft)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SailAreaUpwind)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.GennakerSailArea)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SpinnakerSailArea)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SAOptional)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.SAOptional2)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Weight)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PYS)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ImagePath)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Manufacturers.Manufacturer)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CA.CAName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CrewPersons)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CrewWeight)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DesignBy)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DesignYear)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.OlympicBoat)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.OlympicSectionViewSwitch)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    <a asp-action="EditDinghies" asp-route-id="@item.id">Edit</a> |
                    <a asp-action="DeleteDinghies" asp-route-id="@item.id">Delete</a>
                </td>
            </tr>
    }
        </tbody>
    </table>
    @*</body>
    </html>*@
    

    I hope it will make sense now, as I have mentioned all the code involved in the path of saving and retrieving the data. Thanks for your response.

    Sunday, August 30, 2020 5:41 AM
  • User303363814 posted

    return View(await _context2.Dinghies.ToListAsync());

    In ListDinghies you need to .Include (eager load) the dependent tables

    return View (await _context2.Dinghies.Include(d => d.Manufacturers).Include(d => d.CA).ToListAsync();

    The field called Manufacturers has a plural name but seems to refer to a single thing, that's a bit confusing.  Normally there would be a class in the Model called Manufacturer (singular because each instance represents one thing) which is stored in a table called Manufacturers (plural because it contains many of them).  Does a Dinghy have one Manufacturer or many Manufacturers?  (Names are important!  They help us think about the things which are named)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 30, 2020 6:18 AM
  • User1153554953 posted

    In ListDinghies you need to .Include (eager load) the dependent tables

    return View (await _context2.Dinghies.Include(d => d.Manufacturers).Include(d => d.CA).ToListAsync();

    Oh thank you so so much Paul, it solved the issue, I am very new to asp.net core and making mistakes but at the same time, I am learning lots of things on daily basis by experts like you. I'll surely read up on eager loading.

    Yes, I messed up the naming conventions which I realized later in the development phase, I'll surely take care of it in the future. Two thumbs up

    Sunday, August 30, 2020 6:26 AM
  • User585649674 posted

    It is due to "Lazy loading" feature of Entity framework. Unless you access navigation property, they are not populated from the table.

    I suggest you to create a BusinessDTO class like below

    Public Class DinghiDTO{
      public string DinghiName {get;set;}
      public string ManufacturerName {get;set;}
      public string ClassName {get;set;}
    }

    // change from
    // return View(await _context2.Dinghies.ToListAsync());

    // change to
    var dinghies = await _context2.Dinghies.Select(x=> new DinghiDTO {DinghiName = x.Name, ManufacturerName = x.MAnufacturers.Manufacturer, ClassName = x.CA.CAName}).ToList()
    return View(dinghies)

    // Also change the model of view to List<DinghiDTO>

    Monday, August 31, 2020 9:31 AM