简体   繁体   中英

Using Entity Framework Efficiently

I'm using Entity framework in my ASP.NET MVC project. I need to know that if I'm dealing correctly with the following scenario.

Lets say my Employee table has over 100000 records and I have to apply various filtering according to the client requirement.

So I write 1 method ReadAll() to retrieve all the records from the database and then apply filtering to the datasource using lambda expressions.

Ex: Get employee by ID

public List<Employee> ReadAll()
{
   // return List<Employee>
} 


private Employee(int id)
{
   Employee obj=ReadAll().where(o=>o.empID == id).First();
}

I'm trying to use one read all method because there are various filtering to be applied and I do not have to write separate database access methods to each of them.

Will this affect my application performance adversely?

Change ReadAll to return IQueryable so that it won't execute the query until after you've applied your filter and called First() or ToList().

public IQueryable<Employee> ReadAll()
{
   // return List<Employee>
} 

Entity Framework uses a concept called deferred execution . I encourage you to read up on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM