使用VS2017学习ASP.NET MVC时脚手架报错,请求技术帮助
Hey there! I totally get how frustrating it is when scaffolding throws errors right in the middle of your learning journey—been there, done that with VS 2017 and ASP.NET MVC. Let’s break down the most common fixes for these scaffolding issues, since you mentioned this has happened before and you haven’t been able to resolve it yet.
Common Fixes for Scaffolding Failures
Validate your
UniversityContext.csconfiguration
Scaffolding relies entirely on a properly set up DbContext. Double-check these details:- Ensure your class correctly inherits from
DbContext(not the Core version, since you’re using VS2017 with full .NET Framework) - Confirm your constructor references a valid connection string name, like this:
public class UniversityContext : DbContext { public UniversityContext() : base("name=UniversityContext") { } // Make sure your DbSet properties are defined correctly, e.g.: public DbSet<Student> Students { get; set; } } - All your entity classes should have a clear primary key—either using the
[Key]annotation or following the convention (likeIdorClassNameIdas the property name)
- Ensure your class correctly inherits from
Check NuGet package compatibility and versions
Mismatched or outdated packages are a top culprit for VS2017 scaffolding issues. For ASP.NET MVC with EF6 (the standard for VS2017):- Ensure you have the
EntityFrameworkpackage (notMicrosoft.EntityFrameworkCore) installed at a stable version (6.2.0 is a reliable choice) - Verify
Microsoft.AspNet.Mvcand related packages match your project’s .NET Framework version (4.6.1 or later is recommended for VS2017)
To update: Right-click your project → Manage NuGet Packages → Go to the Update tab and refresh any outdated EF/MVC-related packages.
- Ensure you have the
Clean, rebuild, and restart Visual Studio
Leftover build artifacts often cause weird scaffolding glitches. Follow these steps:- Go to Build → Clean Solution
- Go to Build → Rebuild Solution
- Close and re-open Visual Studio (this old trick fixes so many VS2017 quirks)
- Try running the scaffolding again
Verify your Web.config connection string
Make sure the connection string in yourWeb.configexactly matches the name referenced in your DbContext. It should look like this:<connectionStrings> <add name="UniversityContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=UniversityDB;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>The
nameattribute must be identical to what’s in your DbContext constructor.Get detailed error info with Package Manager Console
If the above steps don’t work, use the console to get verbose error output:- Open Tools → NuGet Package Manager → Package Manager Console
- Run this command (adjust parameters to match your project):
Scaffold-DbContext "YourConnectionStringHere" EntityFramework -OutputDir Models -Context UniversityContext -Verbose
The
-Verboseflag will print the exact root cause of the failure, which will help us pinpoint the issue faster.
If you can share the exact error message you’re seeing and the full code of your UniversityContext.cs, I can give you even more targeted help. But these steps should cover most common scaffolding roadblocks in VS2017 for ASP.NET MVC.
内容的提问来源于stack exchange,提问作者Abrar Hasin Pranto

