2

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

3 Answers 3

1

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.
        };
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.