这是声明实体框架上下文的最佳实践
function()
{
DBContext context = new DBContext();
//Entity code
return ;
}
Run Code Online (Sandbox Code Playgroud)
要么
function()
{
using(DBContext context = new DBContext())
{
//Entity code
}
}
Run Code Online (Sandbox Code Playgroud)
我们是否需要在EntityFrameWork中使用?如果是,我的第二个问题
在DataAccess Layer中我执行EF并将结果存储在IEnumerable里面使用
我的DL
function()
{
IEnumerable something = null;
using(DBContext context = new DBContext())
{
IEnumerable something = ....
}
return something;
}
Run Code Online (Sandbox Code Playgroud)
在控制器中
function()
{
List some = something.ToList();
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我得到这个列表,因为我需要做一些查找操作
"The operation cannot be completed because the DbContext has been disposed Entity Framework"
Run Code Online (Sandbox Code Playgroud)
是的我可以从DL返回一个列表,它工作正常
如果我使用IEnumerable,我该如何处理?