I have a simple db with two tables employee
and empl开发者_高级运维oyeedetails
.
employee
has foreignkey empdetailsid
that points to the table employeedetails
My simple linq query to return employee
var employeeList = from employee in objectcontext.employees select employee;
return employeeList.ToList();
also returns employeedetails and all its contained fields which I do not need. I am using Entity Framework 4.0. Is there a way to avoid getting all the unnecessary data. I could go with defining a model class that just has the employee table fields and could load into that but I was wondering if there was a simpler way. I am using a mysql database.
How do you know that the query returns employee details? Have you checked the generated SQL query, or did you just try to access the EmployeeDetails
object and got the data. In the second case, EF could indeed just be lazy-loading the data on demand (when you try to access it). Otherwise, if you are sure the extra data gets fetched during the execution of the initial query, try implicitly enabling lazy loading before running the query:
objectcontext.ContextOptions.LazyLoadingEnabled = true;
I haven't used ef 4, but most orm's tend to use a concept called lazy loading. The data in the related employee details is only retrieved when a call is made to that object. when you look at the object in your debugger the data for a specific employee details will be retrieved when you click on the object. To get a better idea of what data is being retrieved you will need to run a profiler against the database to trap the sql queries being made. I don't know if mysql has a built in profiler like sql server. If not check out nhprof.
精彩评论