You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

Troubleshooting the EF6 Migration Context Detection Error

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 is public, explicitly inherits from System.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 Demogorgon
    

    Replace Demogorgon.AppDbContext with your actual context's full name.

  • Validate your project's configuration file
    Check your App.config (or Web.config for ASP.NET projects) to ensure:

    1. A valid connection string exists under <connectionStrings>
    2. 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.

  • 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 to Demogorgon. 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:

    1. Right-click the solution → Clean Solution
    2. Right-click the solution → Rebuild Solution
    3. Restart Visual Studio
    4. Run the migration command again

内容的提问来源于stack exchange,提问作者Vanessa Kensington

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 19:17:55