0
I’m trying to insert some data from some files by Entity, but it keeps giving this error:
Method:
internal void AddArquivo(Model.Arquivo arquivo)
{
using (var ctx = new TESTEntities())
{
var versao = arquivo.ArquivoVersoes[0];
ctx.ARQUIVO.Add(new ARQUIVO()
{
ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
ARQUIVO_VERSAO = new ARQUIVO_VERSAO()
{
ARQUIVO_GUID = arquivo.ARQUIVO_GUID,
ARQUIVO_VERSAO_GUID = versao.ARQUIVO_VERSAO_GUID,
ARQUIVO = versao.ARQUIVO,
DATAHORA = versao.DATAHORA,
TAMANHO = versao.TAMANHO,
USUARIO_PESSOA_GUID = versao.USUARIO_PESSOA_GUID
},
DIRETORIO_GUID = arquivo.DIRETORIO_GUID,
EXTENSAO = arquivo.EXTENSAO,
IS_STREAM = arquivo.IS_STREAM,
TAG = arquivo.TAG,
TIPO_DE_ARQUIVO_GUID = arquivo.TipoDeArquivo.TIPO_DE_ARQUIVO_GUID,
ULT_ARQUIVO_VERSAO_GUID = arquivo.ULT_ARQUIVO_VERSAO_GUID,
URL = arquivo.URL,
XARQUIVO = arquivo.XARQUIVO
});
ctx.SaveChanges();
}
}
}
ERRO: "Erro ao atualizar as entradas. Consulte a exceção interna para obter detalhes."
remembering that my primary keys are like varchar, and are not Identity
System.Data.Entity.Infrastructure.DbUpdateException was caught HResult=-2146233087 Message=Erro ao atualizar as entradas. Consulte a exceção interna para obter detalhes. Source=EntityFramework StackTrace: em
System.Data.Entity.Internal.InternalContext.SaveChanges() em InnerException:
System.Data.UpdateException HResult=-2146233087 Message=Erro ao atualizar as entradas. Consulte a exceção interna para obter detalhes. Source=System.Data.Entity StackTrace: em
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) em
System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) em
System.Data.Entity.Internal.InternalContext.SaveChanges() InnerException:
System.Data.SqlClient.SqlException HResult=-2146232060 Message=Violation of PRIMARY KEY constraint 'ARQUIVO_VERSAO_PK'. Cannot insert duplicate key in object TEST.ARQUIVO_VERSAO'. The statement has been terminated. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=14 LineNumber=1 Number=2627 Procedure="" Server=192.168.10.59 State=1 StackTrace: em
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) em
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) em
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) em
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) em
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite) em System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) em
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) em
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() em
System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) em
System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) InnerException:
You over-wrote the
OnModelCreating
ofGEDEntities
? It may be a validation error. Otherwise, you need to go to usInnerException
to see what happened, if it is not detailed enough, try this to show in depth the errors that occurred: http://blogs.infosupport.com/improving-dbentityvalidationexception/– anisanwesley
What’s the mistake? Can you please put it in your question?
– Leonel Sanches da Silva
I didn’t overwrite Onmodelcreating that would be what?
– War Lock
@Warlock, in this case it is important put to Innerexception, ie the one that actually contains useful information about the error. However when it comes to Entity Framework, I will ask you to make a Try with 2 catch, one for Dbentityvalidationexception and one for Exception.
– Tobias Mesquita
As I see this innerexception?
– War Lock
I put a catch for Dbentityvalidationexception, but in debug it already goes to Exception
– War Lock
Put a breakpoint in the Exception, and expand it until you get the innermost exception.
– Tobias Mesquita
I will post the complete error
– War Lock
an example of how to take an Innerexception: https://dotnetfiddle.net/9CMNuR
– Tobias Mesquita
posted the error...
– War Lock
Cannot Insert Duplicate key in Object TEST.ARQUIVO_VERSAO'. The statement has been terminated. You are trying to insert a duplicate Key into the Bank.
– Tobias Mesquita
In this answer teach to capture an internal context exception using Entity Framework.
– Leonel Sanches da Silva
that error is in the method I posted?
– War Lock
@Warlock What is the primary key of
ArquivoVersao
?– Leonel Sanches da Silva
@Warlock, could you reset the entity diagram?
– Tobias Mesquita
ARQUIVO_VERSAO_GUID is the PK, posted the diagram
– War Lock
posting a response.
– Tobias Mesquita