Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库优先与基本实体接口_C#_Entity Framework_Generics_Db First - Fatal编程技术网

C# 数据库优先与基本实体接口

C# 数据库优先与基本实体接口,c#,entity-framework,generics,db-first,C#,Entity Framework,Generics,Db First,环境: 我在DB First Approach工作。我的数据库中有3个表。所有这些表都有状态字段来指示记录状态。这是一个整数字段 场景: 我使用db first方法从这个表中创建了模型图。然后,我为CRUD操作创建了一个通用存储库接口和类 以下是接口 public interface IGenericRepository<T> where T : class { IQueryable<T> GetAll(); Task<T> GetAsync

环境:

我在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();
}
公共接口IGenericRepository,其中T:class
{
IQueryable GetAll();
任务GetAsync(int-id);
无效添加(T实体);
无效删除(T实体);
无效编辑(T实体);
任务SaveAsync();
}
下面是通用存储库类

    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
}
公共抽象类GenericRepository:
IGenericRepository,其中T:类
其中C:MyDBContext,new()
{
公共虚拟IQueryable GetAll()
{
IQueryable查询=_entities.Set();
返回查询;
}
//为了清晰起见,删除了其他方法
}
要求:

在GetAll方法中,我需要检查status字段并只返回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;
        }
}
公共接口IGenericRepository,其中T:class,IBaseEntity
{
//为清晰起见,删除了方法
}
公共抽象类GenericRepository:
IGenericRepository,其中T:类,IBaseEntity
其中C:MyDBContext,new()
{
公共虚拟IQueryable GetAll()
{
IQueryable查询=_entities.Set(),其中(x=>x.Status==1);
返回查询;
}
}
问题:

为此,我们需要将基本接口继承到所有模型。由于模型是从数据库生成的,所以我手动将IBaseEntity添加到EDMXTT文件中的每个模型中

如果我的数据库有任何更改,并且我再次更新了图表,那么添加的界面将被删除


那么DBFirst或my solution中的任何其他替代方法在DB First中都是错误的?

由DB First工具生成的类通常具有
partial
说明符。这意味着您可以通过在不同的文件(由您控制且不会被覆盖)中为同一类添加另一个
partial
类定义来“扩展”定义。接口可以在分部类的单独部分上实现

生成的文件:

public partial class MyEntity {} 
您的单独文件:

public partial class MyEntity : IMyInterface {} 

有关更多信息,请参见此或

这些工具实现了
partial
类。打开一个新的cs文件,为您的类添加另一个
partial
类定义(使用相同的名称),并在其上实现接口。分部类不需要在所有部分上指定接口。看这里,太好了,我知道了。但这里我有一个困惑。这可能很愚蠢。如果删除表并更新模型,则需要手动删除此扩展的分部类。对吗?@akhil如果你完全移除桌子,是的。否则不会。您可以将其他方法和与db无关的属性放在其他文件中,这样它们就不会在每次刷新时被覆盖model@akhil如果这有帮助,请考虑投票或标记绿色复选框。如果您需要进一步澄清,我可以尝试更新答案