나는 다음 (크게 축소 된) 일반 저장소 클래스가 :'where 절'이라는 문자열로 DbSet을 쿼리하는 방법은 무엇입니까?
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private DbSet<TEntity> _entitySet;
private NewExternalsContext _dbContect;
public Repository(NewExternalsContext dbContext)
{
_dbContect = dbContext;
_entitySet = _dbContect.Set<TEntity>();
}
public virtual TEntity Get(object objectId)
{
// TODO Figure out how to use 'id' to build an Expression<Func<TEntity, bool>>.
throw new NotImplementedException();
}
public virtual TEntity FindOne(Expression<Func<TEntity, bool>> where)
{
return _entitySet.FirstOrDefault(where);
}
}
public virtual TEntity Get(object objectId)
방법으로 내 문제는 저장소가 일반적이기 때문에, 내가 TEntity
어떤 id 필드, 또는 무엇을 가지고 있음을 알 수 없다는 것입니다 호출됩니다. 내가 할 수있는 최선은 Id
필드, id 필드에 대한 가장 일반적인 이름이 있는지 확인하는 것입니다. 그런 다음 어떻게해야합니까? _entitySet.Where("Id = " + objectId)
라고 말하세요? 나는 public virtual TEntity FindOne(Expression<Func<TEntity, bool>> where)
메서드를 가지고 있지만, 단지 일반적인 것처럼 id
에 의해 객체를 얻길 원할 때 전체 람다 식을 쓰고 싶지는 않습니다.
DbSet 클래스는 [찾아왔다 방법] (http://msdn.microsoft.com/en-us/library/gg696418(v=103).aspx) 원하는대로 수행하십시오. 기본 키를 기반으로 엔티티를 찾습니다. – nemesv