环境:
我在 DB First Approach 工作。我的数据库中有 3 个表。所有这些表都有状态字段来指示记录状态。这是一个整数字段。
设想:
我使用 db first 方法从该表创建了模型图。然后我为 CRUD 操作创建了一个通用的存储库接口和类。
以下是界面;
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
Task<T> GetAsync(int id);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
Task<bool> SaveAsync();
}
以下是通用的 Repository 类
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class
where C : MyDBContext, new()
{
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
//Removed other methods for clarity
}
要求:
在 GetAll 方法中,我需要检查状态字段并仅返回 value = 1
我目前的解决方案:
由于这是通用存储库,我无法访问方法中的字段。我们可以创建一个带有 Status 字段的基本接口,然后在通用存储库中继承它,并可以在方法中使用该字段。
如下变化;
public interface IGenericRepository<T> where T : class, IBaseEntity
{
//Methods removed for clarity
}
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class, IBaseEntity
where C : MyDBContext, new()
{
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>().Where(x => x.Status== 1);
return query;
}
}
问题:
为此,我们需要继承我所有模型的基本接口。由于模型是从数据库生成的,我手动将 IBaseEntity 添加到 edmx tt 文件中的每个模型中。
如果我的数据库有任何更改并且我再次更新了图表,则会删除手动添加的界面。
那么 DBFirst 中的任何其他替代方法或我的解决方案在 DB First 中是错误的吗?