[英]EF(entity framework) "using" statement and "try catch" withing each other
Is there any difference between these ways of using/try-catch blocks?这些使用/try-catch 块的方式有什么区别吗?
First :第一的 :
public bool Insert(SomeEntity entity)
{
bool result = false;
try
{
using (var db = new MyEntities())
{
db.AddToSomeEntity(entity);
db.SaveChanges();
result = true;
}
}
catch (Exception e)
{
//
}
return result;
}
Second :第二 :
public bool Insert(SomeEntity entity)
{
bool result = false;
using (var db = new MyEntities())
{
try
{
db.AddToSomeEntity (entity);
db.SaveChanges();
result = true;
}
catch (Exception e)
{
//
}
}
return result;
}
Does it affect performance?会影响性能吗?
This string was added in order to satisfy SO submit validation rule.
添加此字符串是为了满足 SO 提交验证规则。
我不认为它违反了任何规则,即你想在什么时候将上下文发送到垃圾收集我建议使用第二种方法,因为它更具体,当你的函数完全执行时,你会将对象发送到垃圾收集,否则您可能会遇到一个异常,即您尝试使用不再可用的上下文
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.