Make Visual Studio 'Add Controller' use Repository Pattern - asp.net-mvc-4

In Visual Studio 2012 with an ASP.net MVC 4 project type, when you add a controller you can make it generate CRUD pages for a selected entity. Is there anyway to get it to use the repository pattern in the controllers as you can when you use the Package Manager to do the same task?
At the moment it news up the data context in the controller.

Use nuget to install the mvcscaffolding package - latest version is 1.0.9.
Now in the list of templates you have also "MvcScaffolding: Controller with read/write action and views, using repositories".

Related

Visual Studio 2022 is not finding codegenerators for scaffolding

Rel: aspnet-codegenerator: No code generators available, Even after adding Microsoft.VisualStudio.Web.CodeGeneration.Design
I have the same issue as presented in the linked question but with Visual Studio 2022 and dotNet 6.
Scaffolding controller using MVC Controller with views, using Entity Framework fails:
However MVC Controller with read/write actions succeeds.
But afterwards adding view for any of the actions likewise fails:
I have performed all actions suggested in the linked post. It looks like I have the right version:
Tool 'dotnet-aspnet-codegenerator' was reinstalled with the latest stable version (version '6.0.2').
The list of available codegenerators looks to include everything:
Update: works from the command line though.

no option for database templates in new item window

I'm creating asp.net core web application and there'is no database templates in new item window. I've installed "Data storing and processing", "Entity framework 6 tools". I'm actually want to create ADO.net template, but it seems to be I need to install something for any database templates to appeared
ADO.net template is removed in Asp.Net core.And NET Framework, as Entity Framework 6 doesn't support . NET Core.So you should use Entity Framework Core rather than Entity framework in Asp.net core.
If you want to use Entity Framework Core with an Existing Database in Asp.net core,you can refer to the official tutorial.
That looks like normal behavior. In .NET Core you'll need to use Entity Framework Core. If you're trying to reference an existing database, you'll need to scaffold it as they've done away with EDMX models in EF Core. Here's how:
If you don't have it already, install the Microsoft.EntityFrameworkCore.Tools NuGet package and any other EF Core packages you think you might need. For example, if you're developing against SQL Server, install the Microsoft.EntityFrameworkCore.SqlServer package.
Run the scaffolding command in the VS Package Manager Console for the database you're trying to add. Something like the following, although you'll want to read the documentation first to determine the parameters you need:
Scaffold-DbContext 'Data Source=(local);Database=MyDB;Trusted_Connection=True' Microsoft.EntityFrameworkCore.SqlServer
This will create a context class for your database that you can then reference throughout your project.

NullReferenceException when I try to add a new ApiController in .NET Core

I see this error message:
When I try to scaffold a controller for and empty API Controller by following these steps:
Right-click on folder
> Add
> Controller...
> Add Scaffold -> API Controller - Empty
> Click Add
Specifications:
Windows 10 Enterprise
Visual Studio Professional 2017 15.6.4
Microsoft .NET Framework 4.7.02556
.NET Core 2.0
Web API ASP.NET Core Web Application
---Edit---
I can't replicate the problem now. One of the suggestions given by #chriss-pratt must have worked (Thank You!) While doing a tutorial, I came across the section to add scaffold tooling and perform initial migration. In case you experience this and the below doesn't work, this part of the tutorial my be promising (if you decide to stick with scaffolding that is).
ASP.NET Core doesn't strictly have an ApiController class. ASP.NET Core doesn't have separate MVC and Web Api components. Everything is just Controller. ApiController is available through a separate "shim" NuGet package, but that's only to make migrating old ASP.NET Web Api code easier. You shouldn't be using it for any new development.
That said, the problem you're having here is with the scaffold. It might be due to the fact that you're trying to scaffold ApiController rather than just Controller. You haven't give any information about what version of Visual Studio you're using or what version of ASP.NET Core.
Try the simple stuff like closing and restarting Visual Studio. If it persists, you can try repairing Visual Studio. Hit the windows key and begin typing "Visual Studio Installer". Open that when it shows up. Below your installed copy of Visual Studio in the list, there will be a drop down menu that looks like three dots stacked on top of each other. There's an option there to "Repair". Start the process and go grab a cup of coffee. It essentially reinstalls Visual Studio, so it'll likely take a while.
All that said, honestly, your best bet is to just don't worry about it. Scaffolding is all but useless anyways. Especially with ASP.NET Core, all you get is a class, which you can easily create yourself. A controller is merely a class that inherits from Controller. Add a new class in your Controllers directory, name it WhateverController and then add : Controller after the name in the code. Then, just start adding your actions and such as normal.

Can I use Nuget to install MVC 4 into Webforms, or is it better to merge MVC config into Webforms, for incremental conversion

Times have changed--now we have Visual Studio 2012, MVC 4, Nuget--and it seems like the procedures in the Hanselman blog and the more recent Channel 9 tutorial could be enhanced. Both Hanselman and Kane recommend using DIFF tools to merge MVC functionality into the Web Forms Project (to endow the webform's .csproj and web.config etc with MVC). However now that Nuget manages packages, I wonder if it would be easier to merely install MVC 4 into the Web Forms project using Nuget; while of course following the procedures in the blog and the channel 9 demo for incrementally upgrading the Webforms project, including dealing with aspx login controls that postback, ascx user controls, and changing base classes Page to ViewPage and MasterPage to ViewMasterPage.
Has anyone tried to use Nuget to endow a Webforms project with MVC 4, and if so, would you recommend that technique?
UPDATE
I am following the Kane method which is an ordered set of guidelines (like modifying a complicated recipe in cooking) and am using NuGet to avoid some tedium. The guideline requires you make a dummy MVC project and compare it to your ASP.NET project to see what you need to add to your original project. You can add stuff using NuGet GUI, but that gets you latest stable assemblies versions which may lead to the dark insane demon of incompatibility. Instead use the NuGet console command line to get the compatible versions listed in the dummy MVC project's packages.config as explained in the following: How to install an older version of package via NuGet?

Upgrading to WebMatrix SimpleMembership

I installed MVC 4 when it was in beta, so I didn't get the SimpleMembership library back then. Now I've built a large project using the old MembershipProvider and was wondering how to upgrade from that to WebMatrix SimpleMembership.
I've already created the database schema and EF model and mapping classes. Now I'm wondering how I can upgrade the rest (install required libraries, etc.)
Is there some upgrade path I can follow, just to bypass screwing up my project?
Here is the upgrade path I used:
Backup everything (Code and SQL)
Create a new Internet Application [with SimpleMembership]
Move following folders from old project to new project:
Scripts
Content
Images
Controllers [don't overwrite AccountController]
Models [don't overwrite AccountModels]
Views
Insert connectionString from old project into Web.config
Run project and create a new user. Then the SimpleMembership SQL tables will be created automatically.
Use Entity Framework Power Tools, right click on project and choose Entity Framework -> Reverse Engineer Code First, to generate all EF entity/mapping classes for all SQL tables.
Delete UserProfile entity and UserProfileMap classes, because they come with SimpleMembership's AccountModels.
Refactor GUID to integer in all project.