简体   繁体   English

可以在Entity Framework的CreateOrUpdate方法中依赖try-catch吗?

[英]Is it ok to rely on a try-catch in a CreateOrUpdate method for the Entity Framework?

Is it acceptable to do this? 这样做是否可以接受? First try to the add the entity. 首先尝试添加实体。 If the add fails, it doesn't matter because that means the entity already exists? 如果添加失败,则无关紧要,因为这意味着实体已存在?

Or is there a more elegant/easy solution? 或者是否有更优雅/简单的解决方案?

EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel();

try
{
    dal.AddToXXXXXX(xxxxxxx);
}
catch
{

}

try
{
    dal.SaveChanges();
    return true;
}
catch
{
    return false;
}

OK I shortened it to... 好吧,我把它缩短为......

EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel();

if(xxxxxxx.ID == 0)
{
    dal.AddToXXXXXX(xxxxxxx);
}

try
{
    dal.SaveChanges();
    return true;
}
catch
{
    return false;
}

It is certainly not OK to do this. 这样做肯定不行 A catch statement with no type in C# means "catch any standard or non-standard exception". C#中没有类型的catch语句意味着“捕获任何标准或非标准异常”。 But you're intent is to prevent a duplicate Add. 但你的意图是防止重复添加。 Adds can fail for a variety of reasons which are not indicative of an existing entry. 添加可能由于各种原因而失败,这些原因并不表示现有条目。 For example, that method could throw a null reference and you'd assume it was added. 例如,该方法可以抛出空引用,并假设它已添加。

If you want to check for a duplicate add, you must catch only the exception that is thrown for a duplicate add. 如果要检查重复添加,则必须仅捕获为重复添加引发的异常。

You would want to start with an IfExists style method, and then skip saving your changes unless you actually have changes. 您可能希望以IfExists样式方法开始,然后跳过保存更改,除非您实际有更改。

As noted by Lucas, try-catch blocks have a large overhead if you fall into the catch block, so generally you don't want to rely on that unless there is no possible way of determining if the item already exists. 正如Lucas所指出的,如果你陷入catch块,try-catch块会有很大的开销,所以通常你不想依赖它,除非没有可能的方法来确定该项是否已经存在。

Don't use a try-catch to do an If statement's job. 不要使用try-catch来执行If语句的工作。 Try-catch is for unusual unanticipated events. Try-catch适用于不寻常的意外事件。

EDIT In your updated code, you are failing to catch an exception that would be thrown by the "AddToXXXXXX" method. 编辑在更新的代码中,您无法捕获“AddToXXXXXX”方法引发的异常。

You should do 你应该做

If(!XXXXXX.Contains(newItemValue))
{
   try
   {
      add...
      savechanges...
   }
   catch
   {

   }
}

alternatively, you could separate out the Add and Savechanges into different try-catch blocks, but that is only necessary if SaveChanges gets executed even when Add fails. 或者,您可以将Add和Savechanges分离到不同的try-catch块中,但这只有在SaveChanges执行时才需要,即使Add失败也是如此。

You could replace the first Try-Catch with an If statement, I think you'll still want the second one though. 你可以用If语句替换第一个Try-Catch,我想你仍然想要第二个。

Edit: It's also not recommended to just catch all exceptions in one block without regard to what they are. 编辑:也不建议只捕获一个块中的所有异常,而不考虑它们是什么。

PS Try Catch blocks use more processing power (time) than If statements. PS Try Catch块比If语句使用更多的处理能力(时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM