I am using ASP.NET MVC 5 with Entity Framework. In several controllers, I need to modify some tables (during a save operation), based on an integer ID
and string ReferenceTable
. Instead of duplicating the code, as well as other code, I want to write a base class, derived off a controller, and then deriving all other controller classes from it.
public class BaseController : Controller
{
protected virtual Boolean SaveTable<TableMap>(MyEntities _context, int ReferenceID, string ReferenceTable) where TableMap : GenericMapTable, new()
{
var GenericTableMap = (from s in _context.Set<TableMap>()
where s.ID == ReferenceID && s.ReferenceTableName = ReferenceTable
select s).ToList();
.... code ....
}
}
The reason I have GenericMapTable
is for it to know the "shape" of the class, as all classes that I will pass it in EF with have both this ID
and ReferenceTable
string.
public abstract class GenericMapTable
{
[Required]
public int ID {get; set;}
[Required]
public string ReferenceTableName {get;set;}
}
In one of the controllers, I try to use this template, and as I am sure you can guess, I am having problems.
public class MyController : BaseController
{
public ActionResult SaveForm(MyViewModel viewModel)
{
Using(MyEntities _context = new MyEntities())
{
... code ...
var result = SaveTable<ConcereteTable>(_context, ReferenceID, ReferenceTable)
.... code ....
}
}
}
My thought is, that in the template class, I need something that describes the class as having ID
and ReferenceTable
, and then deriving my tables from it, but I can't do that, since the classes are part of EF and are auto-generated. For instance, it isn't like I can derive ConcreteTable
from GenericMapTable
.
ConcreteTable
is of course one of several tables that I might pass.
The error I get is:
The type 'Namespace.ConcreteTable' cannot be used as type parameter 'TableMap' in the generic type or method 'BaseController.SaveTable(MyEntities, int, string).' There is no implicit reference conversion from ....
I did some searching, but I can't quite seem to find a good answer on the best practices. This must be fairly common.