How to know without a "remove" in my driver was rejected because of a foreign key violation?

0

how to tell the user that a record has not been deleted because it has other entities that inherit its primary key by means of try / catch?

try
{
    db.usuarios.Remove(usuario);
}
catch(?????)
{
    ???????
}
    
asked by Xique 17.08.2016 в 23:13
source

1 answer

1

If you are not enabled, the cascade deletion should control the exception using something like

try
{
    db.usuarios.Remove(usuario);
}
catch (DbUpdateException ex)
{
    var sqlException = ex.GetBaseException() as SqlException;

    if (sqlException != null)
    {

    }
}

Can not catch SqlException in Entity Framework

by getting the exception of sql you could go through the errors and validate the Number to determine if there are any related to Foreign Key violation

    
answered by 18.08.2016 / 06:31
source