Get all rows using entity framework dbset
The normal way to do this is by instantiating your dbContext.
For example:
public IQueryable<Company> GetCompanies()
{
using(var context = new MyContext()){
return context.Companies;
}
}
There are lots of good tutorials on using CodeFirst Entity framework (which i assume you are using if you have a DbContext and are new)
- http://codefirst.codeplex.com/
- http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx
Set<T>()
is already IQueryable<T>
and it returns all rows from table
public IQueryable<Company> GetCompanies()
{
return DbContext.Set<Company>();
}
Also generated DbContext
will have named properties for each table. Look for DbContext.Companies
- it's same as DbContext.Set<Company>
()
I prefer work on list, also have all relations here
For example:
public List<Company> GetCompanies()
{
using (var context = new MyContext())
{
return context.Companies.ToList();
}
}