C# 从dbcontext中的方法返回泛型表

C# 从dbcontext中的方法返回泛型表,c#,entity-framework,generics,C#,Entity Framework,Generics,如何在DBContext中返回表的泛型类型我尝试了这个,但得到了错误 谁能帮助解决这个问题 public T DeviceExist<T>(string DeviceId, int ImageId) { return DBContext.T.Where(d => d.DeviceId == DeviceId && d.ImageId == ImageId).FirstOrDefault(); } pu

如何在DBContext中返回表的泛型类型我尝试了这个,但得到了错误

谁能帮助解决这个问题

    public  T  DeviceExist<T>(string DeviceId, int ImageId)
    {
     return DBContext.T.Where(d => d.DeviceId == DeviceId && d.ImageId == ImageId).FirstOrDefault();           
    }
public T DeviceExist(字符串DeviceId,int-ImageId)
{
返回DBContext.T.Where(d=>d.DeviceId==DeviceId&&d.ImageId==ImageId).FirstOrDefault();
}

假设您的泛型类型
T
是从具有属性
DeviceId
ImageId
的基类继承的,您可以使用:

DBContext.Set<T>().Where(d => d.DeviceId == DeviceId && d.ImageId == ImageId).FirstOrDefault();
DBContext.Set();

不要忘了在泛型类/存储库的
where
约束中指定
t
从中继承的基类。

您想要的可能是
DbContext.Set()
我想要类似于return DbContext.Set()的东西。其中(d=>d.DeviceId==DeviceId);但是得到错误?没有IntelliScene,我想以返回类型返回泛型类?guyz非常感谢您…最后问题解决如果我从db更新模型,它将生成所有类并删除我的接口实现?我将再次执行Intellicity接口?这太糟糕了?我该如何处理这个问题?我不确定你的要求,以及这是否可能,但考虑到你的情况,最好先切换到EF代码。然后您可以从代码本身而不是相反的方式生成数据库表,这就消除了删除继承链的可能性。同样,我不确定首先切换到代码是否是您的选择。