Ricevo una DbUpdateException
con messaggio
I dati stringa o binari verrebbero troncati
Comprendo che uno dei campi nell'entità non si adatta alla lunghezza della colonna nel database. E che potrei andare giù e controllarli manualmente.
Tuttavia, quello che sto cercando di fare è ottenere un messaggio di errore ragionevole che potrebbe dirmi in quale campo si trova effettivamente! Per esempio
String o binary verrebbero troncati nel campo ProspectName.
Sono in grado di stampare molte informazioni casuali. e ho provato varie cose. Ma nulla indica il nome del campo.
Si prega di notare che questo NON è di tipo DbEntityValidationException
, è un DbUpdateException
// DbUpdateException exception
foreach (var entry in exception.Entries)
{
builder.AppendLine(String.Format("Error at: Type {0}", entry.Entity.GetType().Name));
if ((exception.InnerException is System.Data.Entity.Core.UpdateException) &&
(exception.InnerException.InnerException is System.Data.SqlClient.SqlException))
{
var updateException = (System.Data.Entity.Core.UpdateException)exception.InnerException;
var sqlException = (System.Data.SqlClient.SqlException)exception.InnerException.InnerException;
var result = new List<ValidationResult>();
for (int i = 0; i < sqlException.Errors.Count; i++)
{
builder.AppendLine(String.Format("Error code: {0} ", sqlException.Errors[i].Number));
builder.AppendLine(String.Format("Source: {0} ", sqlException.Errors[i].Source));
builder.AppendLine(String.Format("Message: {0} ", sqlException.Errors[i].Message));
builder.AppendLine(String.Format("State: {0} ", sqlException.Errors[i].State));
builder.AppendLine(String.Format("Procedure: {0} ", sqlException.Errors[i].Procedure));
}
}
}
Errore completo:
I dati stringa o binari verrebbero troncati. La dichiarazione è stata chiusa.
Errore a: Tipo tree_1ECACDBB4458C7A9DEC7CD183FD8B8C3473502FEFFACF160E17AD47718DCE5EA
Codice di errore: 8152
Fonte: .Net SqlClient Data Provider
Messaggio: stringa o dati binari verrebbero troncati.
Stato: 14
Procedura:
Codice errore: 3621
Fonte: .Net SqlClient Data Provider
Messaggio: la dichiarazione è stata chiusa.
Stato: 0
Procedura:
Una soluzione "brutta" (ma funzionale e che utilizza SOLO il codice C #) per trovare esattamente quale proprietà ti sta dando quell'errore sarebbe:
Nel caso si stia facendo un aggiornamento, fare questo:
var myDBObj = db.Mytables.Where(x=>x.Id == myId).FirstOrDefaul();
if(myDBObj == null) return false; // or something else with the error msg
myDBObj.Property1 = myObjToSave.Property1;
db.SaveChanges();
myDBObj.Property2 = myObjToSave.Property2;
db.SaveChanges();
myDBObj.Property3 = myObjToSave.Property3;
db.SaveChanges();
.... // EACH PROPERTY....
myDBObj.PropertyX = myObjToSave.PropertyX;
db.SaveChanges();
Ora, con un punto di frenata o una variabile incrementale per tracciare la "posizione", ecc ... saprai esattamente in questo caso specifico in cui hai l'eccezione ....
NOTA : questa è una brutta soluzione SOLO per rintracciare dove è faillante ... MAI usare questo in produzione ... e ovviamente IMO ci sono altre cose più amichevoli come avere un profiler sql e vedere l'SQL generato e poi provare a eseguilo sullo studio di gestione sql e poi vedi l'errore lì ......
AGGIORNAMENTO # 1
Qualcosa di simile (nota: non l'ho compilato): P
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
string lastPropertyWithError = ""; // You can replace this with a list or so
foreach (PropertyInfo property in properties)
{
try{
property.SetValue(myDBObj, property.GetValue(myObj, null));
db.SaveChanges();
}catch()
{
lastPropertyWithError = property.Name;
}
}
Utilizzando il profiler del framework Entity per acquisire le query sql dal framework dell'entità, trovare la query di aggiornamento ed eseguirla direttamente sul database, si hanno buone possibilità di trovare il campo.