EF启用迁移失败:程序集'Demogorgon'中未找到上下文类型
Hey there, let's tackle this "No context type was found in the assembly 'Demogorgon'" issue you're hitting with EF6 and ASP.NET. I've seen this pop up a few times, so let's go through some targeted fixes that usually resolve it:
First, let's start with the most common root cause: your DbContext setup.
Verify your DbContext class is properly defined
Make sure your context class ispublic, explicitly inherits fromSystem.Data.Entity.DbContext, and has a valid constructor. EF can't detect internal or improperly structured contexts. Here's a valid example:using System.Data.Entity; namespace Demogorgon { public class AppDbContext : DbContext { // Uses the connection string name from your config file public AppDbContext() : base("name=DemogorgonDb") { } // Add DbSet properties for your entities here public DbSet<YourEntity> YourEntities { get; set; } } }Run the migration command with explicit context targeting
Sometimes EF can't auto-scan for the context. Force it by specifying the full namespace and class name in the Package Manager Console:Enable-Migrations -ContextTypeName Demogorgon.AppDbContext -ProjectName DemogorgonReplace
Demogorgon.AppDbContextwith your actual context's full name.Validate your project's configuration file
Check yourApp.config(orWeb.configfor ASP.NET projects) to ensure:- A valid connection string exists under
<connectionStrings> - The Entity Framework configuration section is present. It should look something like this:
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework>If this section is missing, re-installing EF should add it, but double-check after reinstall.
- A valid connection string exists under
Ensure your project builds without errors
Even tiny compilation issues can block EF from scanning your assembly for contexts. Build your project first (Ctrl+Shift+B) and fix any errors before running the migration command.Recheck Package Manager Console settings
Confirm the "Default project" dropdown in Package Manager Console is set toDemogorgon. It's easy to accidentally have it set to a different project if you have multiple in the solution.Clean and rebuild your solution
Cached build files can cause weird detection issues. Try:- Right-click the solution → Clean Solution
- Right-click the solution → Rebuild Solution
- Restart Visual Studio
- Run the migration command again
内容的提问来源于stack exchange,提问作者Vanessa Kensington

