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

14,101

tableName should be something like this for it to work:

class tableName : IValid
{
    // implement IValid
}

Also make sure that the class tableName implements the same IValid interface as used in the method GetValidRecords, i.e. the IValid from the correct namespace.

Share:
14,101

Related videos on Youtube

Sasha
Author by

Sasha

Still learning how to code...

Updated on September 14, 2022

Comments

  • Sasha
    Sasha almost 2 years

    I generated Linq -to -Entity model from database and modified it - I've made interface :

    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()'. There is no implicit reference conversion from 'tableName' to 'IValid'.

    How to fix it?

    EDIT: my table class:

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

    EDIT2: My calling method:

    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.");
    }
    
    • helb
      helb over 10 years
      Make sure tableName is the name of a type implementing IValid.
  • Sasha
    Sasha over 10 years
    I have public partial class tableName: IValid
  • helb
    helb over 10 years
    @Sasha Then make sure it is the same IValid (are there other IValid interfaces in your project?)
  • Sasha
    Sasha over 10 years
    Yes, it's only interface with such name. Can be the Problem, that this table class is generated with Entity Framework?
  • aevitas
    aevitas over 10 years
    As long as it implements the proper IValid interface, then no. Do feel free to show more code in your question such as the actual class that implements IValid - I just tried this construct for myself to double check, and it compiles without problems.
  • helb
    helb over 10 years
    @Sasha Strange. There must be something wrong in the method where you call GetValidRecords. Please post this code.