MvcScaffolding 创建未处置的一次性资源
将 MvcScaffolding 与 EF4.1 一起使用,我发现控制器的第一行生成通常是以下字段定义:
public class FooController : Controller
{
private BarContext context = new BarContext();
//.....
其中 BarContext
如下:
public class BarContext : System.Data.Entity.DbContext
鉴于 System.Data.Entity.DbContext< /code> 实现了
IDisposable
,我很惊讶没有在 FooController
中找到任何可能处理的 Dispose(bool)
方法生成的重写处置上下文
。
这是模板中的疏忽,还是我遗漏了一些东西,使得这不再是问题?
Using MvcScaffolding with EF4.1, I see that the first generated line of a Controller generally is the following field definition:
public class FooController : Controller
{
private BarContext context = new BarContext();
//.....
where BarContext
is as follows:
public class BarContext : System.Data.Entity.DbContext
Given that System.Data.Entity.DbContext
implements IDisposable
, I am surprised not to find any generated override in FooController
for the Dispose(bool)
method that might take care of disposing context
.
Is this an oversight in the templates, or is there something I'm missing that makes this a non-issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您没有丢失任何内容,您确实需要重写 Dispose,如本 EF 教程中所述:
No, you are not missing anything, you do need to override Dispose, as described in this EF tutorial:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application
The built-in scaffolding in MVC 3 Tools Update does generate the override (pre-release versions did not but the released version does), as noted in the tutorial. If the NuGet MvcScaffolding package does not do it, that is an oversight.