声明实体框架上下文使用 [英] Declaring Entity FrameWork Contexts with using
问题描述
这是在声明实体框架上下文
Which is the Best Practise in Declaring Entity FrameWork Contexts
function()
{
DBContext context = new DBContext();
//Entity code
return ;
}
或
function()
{
using(DBContext context = new DBContext())
{
//Entity code
}
}
我们需要使用的EntityFramework使用?如果是我的第二个问题。
Do we need to use using in EntityFrameWork ? If yes my 2nd question
在数据访问层正在执行EF和IEnumerable的存储里面使用
In DataAccess Layer am executing EF and storing the result in IEnumerable inside using
MY DL
function()
{
IEnumerable something = null;
using(DBContext context = new DBContext())
{
IEnumerable something = ....
}
return something;
}
在控制器
function()
{
List some = something.ToList();
}
和在我的控制器正在此为一个列表,因为我需要做一些查找操作正在逐渐
And in my controller am getting this as a list as i need to do some Find operation am getting
"The operation cannot be completed because the DbContext has been disposed Entity Framework"
是的,我可以返回从DL列表和正常工作
Yes i can return a list from DL and it works fine
如何做我处理这个,如果我用IEnumerable的使用?
How do i handle this if i use using with IEnumerable?
推荐答案
您可以通过调用避免延迟加载EF行为 .ToList()
在的IEnumerable <上下文配置前/ code>(即在你的
使用
块)
You can avoid the lazy-loading EF behaviour by calling .ToList()
on the IEnumerable
before the context is disposed (i.e. within your using
block)
这篇关于声明实体框架上下文使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!