In my project using Entity Framework, I have a bunch of functions that look almost exactly alike, so I want to created a generic method they call:
private IHttpActionResult GetData<TEntity>(DbSet<TEntity> data)
The problem I'm having is that the data parameter is saying TEntity has to be a reference type to work, but the type of the entity comes from auto-generated code that doesn't have any base class that I can constrain via a where clause on the method definition.
I'd basically want to call it by getting a context and passing the table in like so:
using (var context = new DataModel.MyEntities()) {
GetData(context.Lab_SubSpace_Contact);
}
private IHttpActionResult GetData<TEntity>(DbSet<TEntity> data) where TEntity : class<=== Why can't you do that?TEntityhas to be a reference type it means that as an unconstrained generic type it can be either a reference or a value type. Igor has given you the constraint that will limit the generic type to a reference typesclass.