0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • It looks like `ConcreteTable` does not derive from `GenericTableMap` – wertzui Mar 08 '18 at 22:37
  • I wonder if [Table-Per-Type](https://stackoverflow.com/questions/22263189/entity-framework-db-first-implement-inheritance) would solve my problem. If this is a possible route, then I would need to modify all of my other calls to EF. – Edwin Rooks Mar 08 '18 at 22:52
  • Yes, ConcreteTable doesn't derive from GenericTableMap @wertzui, which I think is the crux of my problem. How do I use a base class when all of the other classes are auto-generated in EF. – Edwin Rooks Mar 08 '18 at 22:53
  • Switch to Code-First and write your classes so they all derive from your base class. If you cannot do that, take advantage of the fact, that the generated classes are partial classes. – wertzui Mar 09 '18 at 09:36

0 Answers0