Checking whether data persisted successfully

Asked

Viewed 91 times

4

What would be the best way to get one bool indicating whether the method Add() and then SaveChanges() was successfully performed?

Am I trying like this? Is that right?

public bool IncluirRegistro(Pessoa tEntEF)
    {
        db.Pessoa.Add(tEntEF);
        int DadosSalvos = db.SaveChanges();
        return (DadosSalvos > 0);
    }

2 answers

5


It depends on the type of problem you expect to have. If it’s just logic error it seems to me that this is the best way even. It already says if any operation persisted or not.

But it may not have persisted for a number of reasons that the EF itself information through exception, there only capturing an exception to know.

Always capture the expected exceptions that you can do something about. Never capture by capturing and even less capture Exception. Read the documentation to know all possible exceptions in this method. You don’t have to deal with all the exceptions there. It’s very common in almost all the codes that I see here the wrong use of exceptions.

Something like that would be interesting:

public bool IncluirRegistro(Pessoa tEntEF) {
    try {
        db.Pessoa.Add(tEntEF);
        int DadosSalvos = ;
        return (db.SaveChanges() > 0);
    } catch (DbUpdateException e) {
        return false;
    }
}

I put in the Github for future reference.

  • Here would be missing add another catch to collect any other kind of exception.. Otherwise the application would not handle such an exception and breaks the code.

  • @sir_ask You may even need to capture another specific exception, but capture Exception is an error because it is hiding a possible programming error. Programming errors cannot pass beaten. I don’t think the place is right, but I could capture Exception to do something else, like log in the problem and close the application. Programming errors should not be hidden, they should be solved. Programming errors should be addressed in a more general context. You have a lot of information about this: https://answall.com/questions/tagged/exce%C3%A7%C3%a3o? Sort=votes&pageSize=50

  • completely in agreement... he would have to treat the exception, now his goal is to know whether it was successful or not, whether it gave error had not!

  • @Maniero as always busting in the answers. Thank you very much! Just complementing my question, the main goal is to capture bank errors... Type update command fails pq a required field was null, or a key did not exist in the other table. Of course I’m validating the data before sending, but something’s going on.

  • @sir_ask if capture Exception and return false It looks like it’s just a database problem, and it could be a lot more serious. Whenever you do not know what the specific exception is you cannot take a specific action. And without knowing what to do can no longer rely on the execution of the application, the only right thing to do is to break the application as fast as possible, at most it should log in the problem. As rarely this gives problem people neglect it, but when it gives the person does not know how to solve because it even has certain subsidies.There is a lot of application around that seems a Ancient Brasilia

1

With a Try/catch, in case the Add or savechanges fails an exception is released.

public bool IncluirRegistro(Pessoa tEntEF)
    {
try{
        db.Pessoa.Add(tEntEF);
        int DadosSalvos = db.SaveChanges();
return true;
}
catch(Exception e){
return false;
}

    }
  • Thank you very much! Just complementing my question, the main goal is to capture bank errors... Type update command fails pq a required field was null, or a key did not exist in the other table. Of course I’m validating the data before sending, but something’s going on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.