type’T' must be a Reference type in order to use it as Parameter 'Tentity' in the Generic type or method

Asked

Viewed 186 times

0

When trying to make a query using Iqueryable, I get the following error:

Error 3 The type’T' must be a Reference type in order to use it as Parameter 'Tentity' in the Generic type or method 'System.Data.Entity.DbContext.Set()'

1 - I have 3 entities

parent class which is an interface: does not have a base table for it

public interface IParentEquipment
{
    string DaughterOperativeNumber { get; }
}

and two daughter classes that are abstract:

[Table("TB_EQPT_SUB")]
public abstract class EquipmentSub : IParentEquipment
{
    [Column("OPERATIVE_NUMBER")]
    public string OperativeNumber { get; set; }

    public string DaughterOperativeNumber
    {
        get { return this.OperativeNumber ; }
    }
}

[Table("TB_EQPT")]
public abstract class Equipment : IParentEquipment
{
    [Column("OPERATIVE_NUMBER")]
    public string OperativeNumber { get; set; }

    public string DaughterOperativeNumber
    {
        get { return this.OperativeNumber ; }
    }
}

I possess a method to seek one Equipmentsub or Equipment through the operating number.

  public T FindGenericEquipmentByOperativeNumber<T>(string operativeNumber) where T : IParentEquipment
    {
        IQueryable<T> query = _ctx.Set<T>();
        QueryFilter<T> queryFilter = new QueryFilter<T>();
        query = query.Where(s => !(s.DaughterOperativeNumber.Equals(operativeNumber)));
        query = query.Where(queryFilter.GetFilter);
        return query.Single();
    }

I realized that if I change my interface Iparentequipment, for a Abstract the error adds up.

public abstract class IParentEquipment{

}

Why can’t I use an interface in my query?

2 answers

3

Because the signature of the method you are using requires so:

public virtual Microsoft.EntityFrameworkCore.DbSet<TEntity> Set<TEntity> () where TEntity : class;

I put in the Github for future reference.

Documentation.

For some reason I can investigate he needs the adjacent guy to be a class, so he put class as a constraint, then an interface cannot be used directly. It can indirectly, in the type you will use there.

So you have to use a similar restriction, you can use a class or a new(), there will require a concrete type, even if it is characterized by the interface.

2


The mistake happens because the Constraint of the method Set asks the guy T (the type the method expects) is a type is reference (where TEntity : class).

Put an interface on Constraint does not require the type to be by reference, so the error.

To fix, add the Constraint new() or class.

where T : class, IParentEquipment

where T : new(), IParentEquipment

You can read about the restrictions on: Constraints on type Parameters (C# Programming Guide)

  • 1

    the syntax gets: where T : class, IParentEquipment, thanks helped me a lot =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.