Entity Framework 6: Select Error [closed]

1

I'm having problems with Entity Framework 6 with Code First. The "Select" operations give an error and EF6 does not specify the exception, as shown below:

Error Message:

  

An exception was thrown by the destination of a call.

Exception Stack:

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

The insert operation typically occurs:

public void Adicionar(Cliente cliente)
{
    _db.Set<Cliente>().Add(cliente);
    _db.SaveChanges();
}

But the operations below are the ones that throw 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 "Update" operation, because for this, it is necessary to obtain the entity previously in order to be in the context of EF in order to be able to update it.

  • What can this usually be?
asked by anonymous 02.12.2016 / 19:32

1 answer

-2

Next face has it

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

You are only entering the class without any query, so try

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

This OrderBy expression (p => p.Id) will sort the results of your Client class that is being represented by "p=" by searching for the Id "p.Id"

This is another 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);
}

One tip, look for Linq expressions and Lambda expressions in C #. I hope I have helped!

    
03.12.2016 / 03:50