[英]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.请原谅我,因为我对 EF 完全陌生。
Set<T>()
is already IQueryable<T>
and it returns all rows from table Set<T>()
已经是IQueryable<T>
并且它返回表中的所有行
public IQueryable<Company> GetCompanies()
{
return DbContext.Set<Company>();
}
Also generated DbContext
will have named properties for each table.还生成的DbContext
将为每个表命名属性。 Look for DbContext.Companies
- it's same as DbContext.Set<Company>
()查找DbContext.Companies
- 它与DbContext.Set<Company>
() 相同
The normal way to do this is by instantiating your dbContext.执行此操作的正常方法是实例化您的 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)有很多关于使用 CodeFirst Entity 框架的很好的教程(如果你有一个 DbContext 并且是新的,我假设你正在使用)
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.