I can enter records in the bank normally, but when I try to get it, it gives error in EF.
public class Program {
public static void Main(string[] args) {
new ClienteConsole().ConsoleListarCliente(1);
}
}
public class ClienteConsole {
public void ConsoleListarCliente(int matricula)
{
try
{
var dao = new ClienteDao();
var cliente = dao.ObterCliente(matricula);
...
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.WriteLine(e.StackTrace);
}
}
}
public class ClienteDao
{
private MyContext _db = new MyContext();
public void Cadastrar(Cliente cliente)
{
_db.Set<Cliente>().Add(cliente); //Sem erro.
_db.SaveChanges();
}
public Cliente ObterCliente(int matricula)
{
return _db.Set<Cliente>().Find(matricula); //Erro.
}
...
}
An exception was thrown by the destination of a call.
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 MyProject.Infra.Data.Dao.ClienteDao.ObterCliente(Int32 matricula) na D:\Raphael\C#\MyProject\MyProject.Infra.Data\Dao\ClienteDao.cs:linha 28 em MyProject.Console.Console.ClienteConsole.ConsoleListarCliente(Int32 matricula) na D:\Raphael\C#\MyProject\MyProject.Console\Console\ClienteConsole.cs:linha 120
After removing the "try-catch" structure, the following exception is thrown:
InvalidOperationException: The class 'Client' has no parameterless constructor.