简体   繁体   English

类型 '' 不能用作泛型类型或方法 '' 中的类型参数 'T'。 没有从 '' 到 '' 的隐式引用转换

[英]The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to ''

I generated Linq -to -Entity model from database and modified it - I've made interface :我从数据库生成了 Linq -to -Entity 模型并对其进行了修改 - 我制作了界面:

public interface IValid
{
    byte Valid{ get; set; } 
}

and make some generated classes to inherit this interface.并制作一些生成的类来继承这个接口。

I've wrote generic class to access tables from database:我编写了通用类来访问数据库中的表:

public List<T> GetValidRecords<T>() where T: class, IValid
{
    try
    {
        return _context.Set<T>().Where(x => x.Valid == 1).ToList();
    }
    catch (Exception ex)
    {
         throw new Exception(ex.Message);
    }
}

When I call this method in my unit test当我在单元测试中调用此方法时

var records = repositary.GetValidRecords<tableName>();

I get error -我得到错误 -

The type 'tableName' cannot be used as type parameter 'T' in the generic type or method 'GetValidRecords()'.类型“tableName”不能用作泛型类型或方法“GetValidRecords()”中的类型参数“T”。 There is no implicit reference conversion from 'tableName' to 'IValid'.没有从“tableName”到“IValid”的隐式引用转换。

How to fix it?如何解决?

EDIT: my table class:编辑:我的表类:

public partial class tableName: IValid    {
    public byte IsValid { get; set; } 
}

EDIT2: My calling method: EDIT2:我的调用方法:

public void GetValidRecordsGenericTest()
{
    var data = new List<tableName>
    {
        new tableName() {Valid = 1},
        new tableName() {Valid = 1}
    }.AsQueryable();

    var mockSet = new Mock<DbSet<tableName>>();
    mockSet.As<IQueryable<tableName>>().Setup(m => m.Provider).Returns(data.Provider);
    mockSet.As<IQueryable<tableName>>().Setup(m => m.Expression).Returns(data.Expression);
    mockSet.As<IQueryable<tableName>>().Setup(m => m.ElementType).Returns(data.ElementType);
    mockSet.As<IQueryable<tableName>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());           var mockContext = new Mock<Entities>();
    mockContext.Setup(x => x.tableNames).Returns(mockSet.Object);

    var database = new Database(mockContext.Object);
    var records = database.GetValidRecords<tableName>(); // here I get error

    Assert.AreEqual(2, records.Count, "Wrong number of gueltig records.");
}

tableName should be something like this for it to work: tableName应该是这样的:

class tableName : IValid
{
    // implement IValid
}

Also make sure that the class tableName implements the same IValid interface as used in the method GetValidRecords , ie the IValid from the correct namespace.还要确保类tableName实现与方法GetValidRecords使用的相同的 IValid 接口,即来自正确命名空间的IValid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型&#39;&#39;不能用作通用类型或方法&#39;&#39;中的类型参数&#39;&#39;。 没有从“到”的隐式引用转换 - The type '' cannot be used as type parameter '' in the generic type or method ''. There is no implicit reference conversion from '' to '' 该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换 - The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to 该类型不能用作泛型类型或方法中的类型参数“TTo”。 没有隐式引用转换 - The type cannot be used as type parameter 'TTo' in the generic type or method. There is no implicit reference conversion 该类型不能在泛型类型或方法'BaseController <T>'中用作类型参数'T'。没有隐含的参考 - The type cannot be used as type parameter 'T' in the generic type or method 'BaseController<T>'. There is no implicit reference 类型“int”不能用作泛型方法中的类型参数“T”。 'int' 没有拳击转换 - The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int' 类型“ T”不能用作通用类型或方法中的类型参数“ T” - The type 'T' cannot be used as type parameter 'T' in the generic type or method 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 类型&#39;&#39;不能在通用类型或方法中用作类型参数&#39;T&#39; - The type '' cannot be used as type parameter 'T' in the generic type or method 类型“ XXX”不能用作通用类型或方法中的类型参数“ T” - The type 'XXX' cannot be used as type parameter 'T' in the generic type or method 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM