I am trying to implement unit of work and the generic repository pattern as explained here:
My entity Propiedad is like this:
public class Propiedad
{
[Key]
public int Id { get; set; }
public virtual Entidad Entidad { get; set; }
public string Codigo { get; set; }
public string Nombre { get; set; }
public string TipoDeDatos { get; set; }
}
The line where its failing its actually the Index action
public ActionResult Index()
{
return View(unitOfWork.PropiedadRepository.Get(includeProperties:"Nombre, Codigo, TipoDeDatos").ToList());
}
The Generic Repository class:
public class GenericRepository<TEntity> where TEntity : class
{
internal AppDataContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(AppDataContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
and the unit of work class
public class UnitOfWork : IDisposable
{
private AppDataContext context = new AppDataContext();
private GenericRepository<Empresa> empresaRepository;
private GenericRepository<Propiedad> propiedadRepository;
private GenericRepository<Entidad> entidadRepository;
public GenericRepository<Empresa> EmpresaRepository
{
get
{
if (this.empresaRepository == null)
{
this.empresaRepository = new GenericRepository<Empresa>(context);
}
return empresaRepository;
}
}
public GenericRepository<Propiedad> PropiedadRepository
{
get
{
if (this.propiedadRepository == null)
{
this.propiedadRepository = new GenericRepository<Propiedad>(context);
}
return propiedadRepository;
}
}
public GenericRepository<Entidad> EntidadRepository
{
get
{
if (this.entidadRepository == null)
{
this.entidadRepository = new GenericRepository<Entidad>(context);
}
return entidadRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
However I get this error: A specified Include path is not valid. The EntityType '*Propiedad' does not declare a navigation property with the name 'Nombre'.
But the Propiedad entity has a property called Nombre, so I dont get it why its failing.
Include
is made to eagerly load navigation properties, meaning any property that is, in fact, another related entity. Since Nombre is a string, you do not need to include it: it is part of the entity that is returned from the database call. If Nombre were of a class representing a database entity, then you could include it.