I'm using repository pattern in my ASP MVC4 app and I thought it would be nice to create custom scaffold templates to avoid tweaking code every time I generate something.
I followed some tutorials from the web. I copied the CodeTemplates directory form ProgramFiles to my project (only some of the files) and I cleared the Custom Tool property for each.
I managed to achieve 2 goals:
- override the default template for controller: when I go to Controller>Add>Controllers and select "controller with read/write actions and views...." it uses my template.
- add a new view template: when I go to SomeViewFolder>Add>View I can choose my new template there.
The thing I was unable to achieve was to create a new template ControllerWithRepo.tt and be able to select it in Controller>Add>Controllers. Even though I have ControllerWithRepo.tt created it doesn't show up on the template drop down list.
I know it's possible to do what I want by hardcoding things in ControllerWithContext.tt , but it seems to be a lame solution. I would like to do it the right way - to pass a repository class instead of DbContext class and generate the controller the way I want.
Right now If I try to put my repository class as dbcontext it fails to perform the scaffold.
To sum up, how can I create my own controller scaffold template for repository pattern?