views:

60

answers:

3

In the code below all of the fields are returned from the tables except for fields that are foreign keys. How do I get the foreign key fields to display? I just want an equivalent to select * from tableName.

        public ActionResult ShowAllTables() 
    {


        var model = new CSLA_StagingModel()
        {

            depots = db.CSLA_DEPOT.Where(c => c.DEPOT_ID == 10065).ToList<CSLA_DEPOT>(),
            addresses = db.CSLA_ADDRESS.Where(a => a.CSLA_DEPOT.DEPOT_ID == 10065).ToList<CSLA_ADDRESS>(),
        };


        return View(model);

    }

note: I'm using vs2008

EDIT: I think I might use Linq to SQL until I get VS2010

+1  A: 

There is no SELECT *.

You can, however, project the IDs:

var q = from a in db.CSLA_ADDRESS
        select new
        {
            ADDRESS_ID = a.ADDRESS_ID,
            DEPOT_ID = a.CSLA_DEPOT.DEPOT_ID,
            // etc.
        };
Craig Stuntz
+1  A: 

In Entity Framework 3.5 and VS 2008, foreign keys are hided under their Navigation properties (aka Independent Association) and are not directly accessible through the target entity object. So in order to access them you need to do it through their navigation properties (e.g. CSLA_DEPOT) like the code Craig has posted.

Morteza Manavi
A: 

I would add the FK field as a scaler property on the entity. If this FK VALUE holds business meaning then there is nothing wrong with adding it to your conceptual model. This will also make the code discrete and readable.

Slappy