Com*_*ity 1 .net c# dispose entity-framework
我正在编写一个项目,我使用Microsoft Code Analysis,但确实收到以下错误:
CA2000:在丢失范围之前处置对象.
这是我在实体框架周围编写的代码.
public bool IsInstalled(InstallationContext context)
{
var dbContext = new ScheduleFrameworkDataContext();
var repository = new TaskRepository(dbContext);
try
{
// Check if there is already a task with the same name.
if (repository.Get().Select(x => x.Name == context.InstallationParameters.Name).Any())
{ return true; }
}
finally { dbContext.Dispose(); }
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在,我认为我的上下文被处理掉了,因为它在finally块中.(上下文是EF Code First DB Context).但是,我仍然收到这个错误.
我在这里错过了什么吗?
在这种情况下,代码分析工具是正确的.
如果TaskRepository()构造函数抛出,则finally块将不会运行(因为异常被抛出try块外),并且dbContext不会被丢弃.
移动构造函数调用,并分配给repository内部的try块将抑制警告.
| 归档时间: |
|
| 查看次数: |
196 次 |
| 最近记录: |