C# 使用 using 声明实体框架上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16058769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring Entity FrameWork Contexts with using
提问by user2067567
Which is the Best Practise in Declaring Entity FrameWork Contexts
声明实体框架上下文的最佳实践是什么
function()
{
DBContext context = new DBContext();
//Entity code
return ;
}
or
或者
function()
{
using(DBContext context = new DBContext())
{
//Entity code
}
}
Do we need to use using in EntityFrameWork ? If yes my 2nd question
我们需要在 EntityFrameWork 中使用 using 吗?如果是,我的第二个问题
In DataAccess Layer am executing EF and storing the result in IEnumerable inside using
在数据访问层正在执行 EF 并将结果存储在 IEnumerable 内部使用
MY DL
我的DL
function()
{
IEnumerable something = null;
using(DBContext context = new DBContext())
{
IEnumerable something = ....
}
return something;
}
In Controller
在控制器中
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"
Yes i can return a list from DL and it works fine
是的,我可以从 DL 返回一个列表,它工作正常
How do i handle this if i use using with IEnumerable?
如果我与 IEnumerable 一起使用,我该如何处理?
采纳答案by paul
You can avoid the lazy-loading EF behaviour by calling .ToList()on the IEnumerablebefore the context is disposed (i.e. within your usingblock)
你可以通过调用避免延迟加载EF行为.ToList()在IEnumerable之前的背景是设置(即你的内using框)
回答by MaxSC
Your request will be executed toward the datasource as soon as you'll call .ToList() method.
只要您调用 .ToList() 方法,您的请求就会向数据源执行。
That's why you cannot perform .ToList() in your Controller as your context as been disposed at the end of the using block.
这就是为什么你不能在你的控制器中执行 .ToList() 作为你的上下文,因为它在 using 块的末尾被处理。
In your DL method, just do something like:
在您的 DL 方法中,只需执行以下操作:
IEnumerable<Something> function()
{
using(DBContext context = new DBContext())
{
return something.ToList();
}
}
and in your Controller you'll get an IEnumerable of Something:
在你的控制器中,你会得到一个 IEnumerable 的东西:
var mySomethingIEnumerable = DL.Function();
Hope that helps!
希望有帮助!
回答by kingdango
Yes, a using is the best practice because it cleans up your context. The Using statementis a shortcut for:
是的,使用是最佳实践,因为它可以清理您的上下文。在使用的语句是一个快捷方式:
try {
// Execute your code inside the using statement
}
finally {
// Cleanup the context no matter what by calling .Dispose()
}
Keep in mind, your context likely returns IEnumerables and since EF supports lazy loading these objects won't be populated until you fetch them to a concrete collection (ie. yourResult.ToList()).
请记住,您的上下文可能会返回 IEnumerables,并且由于 EF 支持延迟加载,因此在您将它们提取到具体集合(即 yourResult.ToList())之前,不会填充这些对象。
A common negative outcome occurs in this scenario:
在这种情况下会出现一个常见的负面结果:
public IEnumerable<Employee> GetEmployeesInAccounting()
{
using(var myContext = new MyDbContext())
{
return myContext.Employees.Where(emp => emp.Department == 'Accounting');
}
}
// Code that fails, Assuming Manager is a lazy loaded entity, this results in an exception but it compiles no problem
var acctEmps = GetEmployeesInAccounting();
var something = acctEmps.First().Department.Manager.Department;
You can avoid this by using the .Include(emp => emp.Manager)(linq extension method) and binding your result using .ToList();
您可以通过使用.Include(emp => emp.Manager)(linq 扩展方法) 并使用绑定您的结果来避免这种情况.ToList();

