linq to sql - MVC 2 with Entity Data Model returning Null for foreign keys -
i creating first mvc 2 applicaiton. i've followed expamples in books have , created ado.net entity data model auto generated model.edms , model.designer.cs files. created repository.cs file in model folder store methods retrieving data. when use theses methods retrieve object tables foriegn key attributes returned null. here 1 of methods
private lantracerentities2 entities = new lantracerentities2(); public employee findemployee(string empid) { var emp = employee in entities.employees employee.login == empid select employee; return emp.firstordefault(); } the employee table has following columns: id empfname emplname empinitial phone login email locid
locid foriegn key linking location table. when run method returns value every attribute locid. locid null. there data in table. not object have returning null foriegn key attributes. how can method return fk values?
it looks comparing wrong property? given name of parameter i think might want compare id of employee:
var emp = employee in entities.employees employee.id == empid select employee; having said primary key should numeric int/bigint - if that's case instead:
int id = convert.toint32(empid); var emp = employee in entities.employees employee.id == id select employee; if want include location part of query results can ask that:
var emp = employee in entities.employees.include("location") .. this should populate location property in employee record when retrieve it.
Comments
Post a Comment