I put SaveChanges()
method inside a try/catch block, but I couldn't catch SqlExeption.
try
{
db.SaveChanges();
}
catch (Exception ex)
{
}
SqlException
is System.Data.SqlClient.SqlException class so it's normal that you can't catch this exception
The EntityFramework DbContext.SaveChanges Method()
can throw the following exceptions only as by MSDN
DbUpdateException
DbUpdateConcurrencyException
DbEntityValidationException
NotSupportedException
ObjectDisposedException
InvalidOperationException
So you can do something like this for example
try
{
db.SaveChanges();
}
catch (DbUpdateException ex)
{
}
catch (DbUpdateConcurrencyException ex)
{
}
More
The exceptions mentioned above are entity Framework Customized exceptions that only EF
is responsible on When and How to trigger Them take a look at Implementing custom exceptions