getById 函数在存储库模式中获取一个或多个键
getById function the takes one or multiple key within the repository pattern
所以我使用带有 asp.net 核心和 entity framework 6 的存储库模式从服务层访问数据库。问题是我有一个具有复合键的实体,而通用存储库将 int 作为参数,因此我无法将多个变量传递给 find()。
我尝试将 int 更改为对象并在需要时发送带有 2 个 ID 的对象,但这似乎不起作用。
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById( int id);
void Update(T entity);
void Create(T entity);
void Delete(T entity);
}
你可以使它与 find 方法的签名相同
public virtual TEntity Find(params object[] keyValues);
接受键值作为 params
类型 object
所以会是
T GetById(params object[] keyValues);
所以在这种情况下,它将接受任意数量的任意类型的主键
例如
new customerRepo().GetById(123)
new customerRepo().GetById(123,1)
等等
所以我使用带有 asp.net 核心和 entity framework 6 的存储库模式从服务层访问数据库。问题是我有一个具有复合键的实体,而通用存储库将 int 作为参数,因此我无法将多个变量传递给 find()。
我尝试将 int 更改为对象并在需要时发送带有 2 个 ID 的对象,但这似乎不起作用。
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById( int id);
void Update(T entity);
void Create(T entity);
void Delete(T entity);
}
你可以使它与 find 方法的签名相同
public virtual TEntity Find(params object[] keyValues);
接受键值作为 params
类型 object
所以会是
T GetById(params object[] keyValues);
所以在这种情况下,它将接受任意数量的任意类型的主键
例如
new customerRepo().GetById(123)
new customerRepo().GetById(123,1)
等等