I'm trying to reproduce this example of EntityFramework with standard repository, as follows the link: link
But it is giving the error described in the topic title.
I'll post the codes.
The first is the class BancoContext.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Repositorio.Entidades;
namespace Repositorio.DAL.Contexto
{
public class BancoContexto : DbContext
{
public BancoContexto() : base("ConnDB") { }
}
}
The second is the IRepository.cs interface:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repositorio.DAL.Repositorios.Base
{
interface IRepositorio<TEntity> where TEntity : class
{
IQueryable<TEntity> GetAll();
IQueryable<TEntity> Get(Func<TEntity, bool> predicate);
TEntity Find(params object[] key);
void Atualizar(TEntity obj);
void SalvarTodos();
void Adicionar(TEntity obj);
void Excluir(Func<TEntity, bool> predicate);
}
}
The third is the class Repository.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Repositorio.DAL.Contexto;
namespace Repositorio.DAL.Repositorios.Base
{
public abstract class Repositorio<TEntity> : IDisposable,
IRepositorio<TEntity> where TEntity : class
{
BancoContexto ctx = new BancoContexto();
public IQueryable<TEntity> GetAll()
{
return ctx.Set<TEntity>();
}
public IQueryable<TEntity> Get(Func<TEntity, bool> predicate)
{
return GetAll().Where(predicate).AsQueryable();
}
public TEntity Find(params object[] key)
{
return ctx.Set<TEntity>().Find(key);
}
public void Atualizar(TEntity obj)
{
ctx.Entry(obj).State = EntityState.Modified;
}
public void SalvarTodos()
{
ctx.SaveChanges();
}
public void Adicionar(TEntity obj)
{
ctx.Set<TEntity>().Add(obj);
}
public void Excluir(Func<TEntity, bool> predicate)
{
ctx.Set<TEntity>()
.Where(predicate).ToList()
.ForEach(del => ctx.Set<TEntity>().Remove(del));
}
public void Dispose()
{
ctx.Dispose();
}
}
}
The fourth is the ClientRepository.cs class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repositorio.DAL.Repositorios.Base;
using Repositorio.Entidades;
namespace Repositorio.DAL.Repositorios
{
public class ClienteRepositorio : Repositorio<Cliente>
{
}
}
The fifth is the class Cliente.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Repositorio.Entidades
{
public class Cliente
{
public int ClienteID { get; set; }
public string Nome { get; set; }
public string CNPJ { get; set; }
public string Endereco { get; set; }
public string Telefone { get; set; }
public bool Ativo { get; set; }
}
}
The last is the console class, Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Repositorio.DAL.Repositorios;
using Repositorio.Entidades;
namespace Repositorio.Console
{
class Program
{
static void Main(string[] args)
{
Adicionar();
}
private static void Adicionar()
{
using (var repClientes = new ClienteRepositorio())
{
new List<Cliente>
{
new Cliente { Nome="Microsoft", Ativo=true, CNPJ="9394.4343/0001-55",
Endereco="1, Microsoft One", Telefone="11574739494"},
new Cliente { Nome="Google", Ativo=true, CNPJ="1234.9494/0001-33",
Endereco="12, Santa Clara, CA", Telefone="1185858483"},
new Cliente { Nome="Oracle", Ativo=true, CNPJ="9876.4433/0002-44",
Endereco="54, Santa Monica", Telefone="4884848592"}
}.ForEach(repClientes.Adicionar);
repClientes.SalvarTodos();
System.Console.WriteLine("Clientes adicionadas com sucesso.");
System.Console.WriteLine("======= clientes cadastrados ===========");
foreach (var c in repClientes.GetAll())
{
System.Console.WriteLine("{0} - {1}", c.ClienteID, c.Nome);
}
System.Console.ReadKey();
}
}
}
}
I'll leave the App.config file for the layer where the console class is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="ConnDB" connectionString="Data Source=joao\sqlexpress;Initial Catalog=db_repositorio;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>