1
I’m having problems with Entity Framework 6 with Code First. The operations of "Select" give error and EF6 does not specify the exception, as shown below:
Error message:
An exception was triggered by the destination of a call.
Pile of Exception:
em System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) em System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) em System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(Translator translator, Type elementType, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer) em System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator) em System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) em System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6() em System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) em System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5() em System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) em System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) em System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() em System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() em System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source) em System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence) em System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot) em System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) em System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source) em System.Data.Entity.Internal.Linq.InternalSet`1.FindInStore(WrappedEntityKey key, String keyValuesParamName) em System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues) em System.Data.Entity.DbSet`1.Find(Object[] keyValues) em ProjetoTeste.Infra.Data.Repositories.ClienteRepository.ObterCliente(Int32 id) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\ProjetoTeste.Infra.Data\Repositories\ClienteRepository.cs:linha 43 em ProjetoTeste.Domain.Services.ClienteService.ObterCliente(Int32 id) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\ProjetoTeste.Domain\Services\ClienteService.cs:linha 29 em ProjetoTeste.Application.Services.ClienteAppService.ObterCliente(Int32 id) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\ProjetoTeste.Application\Services\ClienteAppService.cs:linha 30 em ProjetoTeste.Web.Controllers.ClienteController.Details(Int32 id) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\ProjetoTeste.Web\Controllers\ClienteController.cs:linha 93
Insertion operation normally occurs:
public void Adicionar(Cliente cliente)
{
_db.Set<Cliente>().Add(cliente);
_db.SaveChanges();
}
But the operations below are the ones that launch the exception:
public ICollection<Cliente> ObterTodos()
{
return _db.Set<Cliente>().ToList();
}
public Cliente ObterCliente(int id)
{
return _db.Set<Cliente>().Find(id);
}
I did not test the operation of "Update", because, for this, it is necessary to obtain the entity in advance for it to stay in the context of the EF in order to update it.
- Which, normally, could be?
Your _db is what? has no way to Return _db.Cliente.Tolist();
– Marco Souza
"_db" is the context.
– Raphael
In any of these methods you use
try-catch
?Repositories.ClienteRepository.ObterCliente
,Domain.Services.ClienteService.ObterCliente
,Application.Services.ClienteAppService.ObterCliente
orControllers.ClienteController.Details
?– Jéf Bueno
I use the default repository, and leave the "Try-catchs" in the controller.
– Raphael
Yes, remove the repository that solves.
– Leonel Sanches da Silva