问题描述
我的大多数应用程序都使用实体框架,但是它们都是代码优先的.
I use Entity framework for most of my applications, but they are all code-first.
现在我正在处理一个现有的数据库.
Now I am dealing with a pre existing database.
我能够通过DbContext查找,更新和添加记录.
I am being able of finding, updating, and adding records through DbContext.
public class CompanyService
{
public void AddCompany(Company cpn)
{
CompanyModel cm = new CompanyModel();
cm.Companies.Add(cpn);
cm.SaveChanges();
}
public void EditCompany(Company cpn)
{
CompanyModel cm = new CompanyModel();
company existing = cm.Companies.Find(cpn.id);
cm.Entry(existing).CurrentValues.SetValues(cpn);
cm.SaveChanges();
}
public List<Company> List()
{
CompanyModel cm = new CompanyModel();
}
}
CompanyModel
是一个这样的类,例如, CompanyModel:DbContext
,
CompanyModel
is a class such that CompanyModel : DbContext
, my bad.
但是 CompanyModel
非常有限,它没有 Where
方法.
But CompanyModel
is very limited, it does not have a Where
method.
如何获取公司
的列表.
推荐答案
使用代码文件顶部的System.Linq; 可能会丢失.这将使您能够使用Linq扩展名,例如
ToList()
.
You are probably missing using System.Linq;
at the top of your code file. This will enable you to use the Linq extensions like ToList()
.
这篇关于实体框架,获取数据库第一种情况的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!