Object selection does not subselect related objects

I'm learning ASP.NET MVC (and MVC in general) and all the online help I find demonstrates with just one table, not relationships between multiple tables. I am running a query that I expect should also return related objects, but that is not the case and cannot figure out what I am missing. I appreciate your help!

I have the following data:

Ticket
   TicketID  CompanyID  Subject  ...
   --------  ---------  -------
   1         1          "stuff"
   2         1          "things"

Company
   CompanyID  Name      ...
   ---------  --------
   1          "FredCo"

      

Thus, each ticket is associated with a specific company. I am trying to create a Details view for tickets and I want to display the company name. Here is what I have installed now.

Model

A Ticket

and an object Company

, and there is an association between them called CompanyTicket

. The navigation properties are Ticket.Company and Company.Tickets.

In the mapping data for the association, I:

Maps to Ticket
    Company.CompanyID <-> CompanyID
    Ticket.TicketID   <-> TicketID

      

controller

My TicketController.Details method looks like this:

public ActionResult Details( int id )
{
    var tickettoview = ( from m in _db.Ticket
                         where m.TicketID == id
                         select m ).First();
    return View( tickettoview );
}

      

Edit . After going through Mark Hamilton's suggestion, I realize that the problem lies with my request. Setting a checkpoint for a return shows that it tickettoview

has never been filled in by the Client. So this narrows it down, but I'm still not sure how to populate the Company attribute.

Thanks again!

+1


a source to share


2 answers


Matt's answer gave me the dictionary I need to do this and eventually led me to this page . Adding .Include("Company")

to my dataset brought in a company property.



var tickettoview = ( from t in _db.Ticket.Include("Company")
                     where t.TicketID == id
                     select t ).First();

      

+2


a source


This seems to be the same problem I had when I asked this question . See the answer I posted which describes how I fixed it.



I've used Linq for SQL, not Linq for Entities, but I'm guessing the DataLoadOptions and LoadWith methods (or something like that) still work. Someone who knows about Entity Framework can correct me if I'm wrong.

+1


a source







All Articles