Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all rows using entity framework dbset

I want to select all rows from a table using the following type of syntax:

public IQueryable<Company> GetCompanies() {     return DbContext.Set<Company>()     .// Select all } 

Forgive me as I am completely new to EF.

like image 297
Wesley Skeen Avatar asked Dec 01 '12 11:12

Wesley Skeen


2 Answers

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>()

like image 92
Sergey Berezovskiy Avatar answered Oct 06 '22 09:10

Sergey Berezovskiy


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
like image 21
Not loved Avatar answered Oct 06 '22 08:10

Not loved