我正在尝试实现工作单元和通用存储库模式,如下所述:
我的实体Propiedad是这样的:
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; }
}
其实际上未能使用Index操作的行
public ActionResult Index()
{
return View(unitOfWork.PropiedadRepository.Get(includeProperties:"Nombre, Codigo, TipoDeDatos").ToList());
}
Generic Repository类:
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;
}
}
和工作单位
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);
}
}
但是我收到此错误: 指定的包含路径无效。 EntityType&#39; * Propiedad&#39;不会声明名称为&#39; Nombre&#39;。
的导航属性但是Propiedad实体有一个名为Nombre的属性,所以我不明白为什么它失败了。
答案 0 :(得分:3)
Include
会急切地加载navigation properties,这意味着任何属性,实际上都是another related entity。由于Nombre是一个字符串,因此您不需要包含它:它是从数据库调用返回的实体的一部分。如果Nombre是表示数据库实体的类,那么您可以包含它。