I'm trying to create a generic controller on my C#/MVC/Entity Framework application.
public class GenericRecordController<T> : Controller
{
private DbSet<T> Table;
// ...
public action()
{
// ...
db.Entry(T_Instance).State = System.Data.Entity.EntityState.Modified;
}
}
However the DbSet<T>
and T_Instance
line has a compiler error.
The type T
must be a reference type in order to use it as parameter.
When I constrain it as a class
, it was solved.
Controller where T : class
What does the error mean? I'm not asking for a solution, I would like to understand why this error occurs and why constraining it as a class
solves it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…