Entity Framework 6: Select Error

Asked

Viewed 386 times

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();

  • "_db" is the context.

  • In any of these methods you use try-catch? Repositories.ClienteRepository.ObterCliente, Domain.Services.ClienteService.ObterCliente, Application.Services.ClienteAppService.ObterCliente or Controllers.ClienteController.Details?

  • I use the default repository, and leave the "Try-catchs" in the controller.

1 answer

-2

Next guy gets into this

public ICollection<Cliente> ObterTodos()
{
    return _db.Set<Cliente>().ToList();            
}

You’re just coming into the class without making any queries so try

public ICollection<Cliente> ObterTodos()
{
    return _db.Set<Cliente>().OrderBy(p=> p.Id).ToList();            
}

this expression Orderby(p=> p.Id) will organize in ascending order the results of its Client class that is represented by "p=" searching for the Id "p.Id"

is in that other function:

public Cliente ObterCliente(int id)
{
    return _db.Set<Cliente>().Find(id);
}

would look like this:

public Cliente ObterCliente(int id)
{
    return _db.Set<Cliente>().Find(p=> p.Id == Id);
}

A hint, look for Linq expressions and Lambda expressions in C#. I hope I’ve helped!

  • Thanks Samuel, but he also made the same mistake. I have no idea what it might be, because I have another project that has the same configs and has no problem. I’ll have to redo the whole project, maybe it’ll work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.