Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC View Scaffolding not working with Generic base class?

Is there a reason the built in MVC View Scaffolding in Visual Studio 2015 does not work with inherited base classes that contain a generic id? Simple test case:

public abstract class BaseEntity
{
}

public abstract class Entity<TKey> : BaseEntity
{
    public TKey Id { get; set; }
}

public class Country : Entity<int>
{
    public string Name { get; set; }
    public string CountryCode { get; set; }
}

Trying to create a scaffolded view (e.g. List, Create, Edit, Delete) using the Country entity results in the following error pop-up:

There was an error running the selected code generator: 'The method or operation is not implemented.'

If I remove the TKey type parameter and make Entity non-generic by defining a fixed type for Id, I can then scaffold the views.

I know in this simple case, I'm not saving myself much work by having the generic base class. I'm also aware that the "best practice" is to use View Models instead of Domain Models in your views. However, I'd like to understand why using a base class with a generic type is causing an issue with the scaffolding.

like image 996
Sam Avatar asked Oct 19 '22 21:10

Sam


1 Answers

I also experienced that bug and found out it was already reported on Microsoft Connect. The only options we seem to have for now are:

  • Generating views from the controller by temporary removing inheritance (not a great idea though but could avoid loosing a lot of time)
  • Waiting for a fix from Microsoft

Note: It seems the same problem affects Visual Studio 2013 (Update 5) but I didn't find any bug tracker entry for that.

Should you want to follow the advancement of the bug fix or provide further information to the support team, you can pay a visit to Microsoft's bug tracker here: https://connect.microsoft.com/VisualStudio/feedback/details/2187798/mvc-view-scaffolding-not-working

like image 143
WebManiaK Avatar answered Oct 21 '22 17:10

WebManiaK