【问题标题】:Apply Migrations for EF 6 Code First - no context type was found为 EF 6 Code First 应用迁移 - 未找到上下文类型
【发布时间】:2020-08-22 12:04:36
【问题描述】:

我正在开发 WCF .NET Framework 4.8 项目,它的 WCF 自托管在控制台应用程序中。

我已经在 WCF 控制台应用程序引用的他们自己的单独项目中设置了定义的 POCO 类和 DbContext

我已经在主控制台应用程序的 app.config 中为 DbContext 声明了 连接字符串

当我尝试执行 EnableMigrationsAdd-Migration 等 EF Code First 命令时,它们无法执行并出现错误:

No context type was found in the assembly 'MAL.App.ConsoleHost'

控制台应用程序 App.Config

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
</entityFramework>
<connectionStrings>
   <add name="AppDbContext" connectionString="Data Source=MySQLEXPRESS;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

数据类库

public class AppDbContext: DbContext
{
    public DbSet<Album> Albums { get; set; }
    public DbSet<AlbumType> AlbumTypes { get; set; }
    public DbSet<Artist> Artists { get; set; }
}

一个 POCO 类

public class Album
{
    public Guid AlbumID { get; set; }
    public string AlbumName { get; set; }
    public Guid ArtistID { get; set; }
    public Guid AlbumTypeID { get; set; }
    public int Stock { get; set; }

    public Artist Artist { get; set; }
    public AlbumType AlbumType { get; set; }

}

控制台主程序

static void Main(string[] args)
{
    ServiceHost hostMusicService = new ServiceHost(typeof(MyWebServices));
    hostMusicService.Open();

    Console.WriteLine("Web Services Started. Press [Enter] To Exit  ");
    Console.ReadLine();

    hostMusicService.Close();
}

【问题讨论】:

    标签: c# entity-framework-6 ef-code-first .net-4.8


    【解决方案1】:

    当您的 DbContext 在与主运行时分开的项目中定义并且您的主运行时在解决方案中设置为 默认项目 时,会发生此错误。

    首次打开包管理器控制台时,它假定解决方案默认项目是您要对其执行命令的项目,例如Enable-Migrations

    您可以更改解决方案的 默认项目,但更简单的解决方法是将包管理器控制台中的默认项目更改或设置为具有您的 AppDbContext 定义的项目,因为这是您希望在其中管理 Migrations 的项目。

    这里有一个问题,仍然将 Default Project 作为控制台应用程序留在 Solution 中。

    • 解决方案默认项目不仅仅是在您按 F5 运行时将执行的项目,它是 Visual Studio 需要访问应用程序/Web 配置的任何时候使用的项目运行时的文件。
    • 这对于带有 EDMX 设计表面的早期版本的 EF 是相同的,甚至在使用 DataSet 设计器之前,默认项目配置文件中的连接字符串是 VS 在设计时使用的字符串时间。

    您还可以在大多数 EF CLI 命令中使用-ProjectName 参数来传递项目名称,而不是使用默认名称。为简洁起见,我通常将此保留用于有多个项目具有上下文的场景,或者特别是当同一个项目中有多个上下文时。您不想每次都输入项目名称Add-MigrationUpdate-DataBase 或任何其他类似这样的脚手架命令。

    要获取有关受支持参数的信息,请在控制台中的命令后使用-? 开关:

    PM> Enable-Migrations -?
    
    NAME
        Enable-Migrations
        
    SYNOPSIS
        Enables Code First Migrations in a project.
        
        
    SYNTAX
        Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations] [-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName <String>] [-ContextProjectName <String>] [-ConnectionStringName <String>] 
        [-Force] [-ContextAssemblyName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>]
        
        Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations] [-MigrationsDirectory <String>] [-ProjectName <String>] [-StartUpProjectName <String>] [-ContextProjectName <String>] -ConnectionString <String> 
        -ConnectionProviderName <String> [-Force] [-ContextAssemblyName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>]
        
        
    DESCRIPTION
        Enables Migrations by scaffolding a migrations configuration class in the project. If the
        target database was created by an initializer, an initial migration will be created (unless
        automatic migrations are enabled via the EnableAutomaticMigrations parameter).
        
    
    RELATED LINKS
    
    REMARKS
        To see the examples, type: "get-help Enable-Migrations -examples".
        For more information, type: "get-help Enable-Migrations -detailed".
        For technical information, type: "get-help Enable-Migrations -full".
    
    PM> 
    

    【讨论】:

    • 嗨,克里斯,我需要先运行迁移,然后才能使用 MyDbContext,但我无法这样做。我确信控制台应用程序并且在这个项目中没有任何网络前端。我已经添加了上述错误的屏幕截图
    • 找到了答案。请参考以上内容
    • 也可以,但是每次都要输入很多,只需在包管理器控制台中设置默认项目即可简化命令行交互
    • 这是没有太多文档记录的事情之一,主要是因为大多数示例都被简化为单个项目,我建议使用 EF 上下文构建解决方案和和你一样在不同项目中的 POCO。一旦你弄清楚了这些事情,你就不会忘记它
    【解决方案2】:

    我找到了答案。您需要在迁移中添加数据类库项目名称,如下所示;

    - Enable-Migrations -ProjectName MyContextProjectNameHere -StartUpProjectName MyStartUpProjectNameHere -Verbose
    
    - add-migration Initial -ProjectName MyContextProjectNameHere
    
    -  update-database -ProjectName MyContextProjectNameHere
    

    【讨论】:

      【解决方案3】:

      试试这个命令可能会有所帮助。

      EntityFrameworkCore\启用迁移

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      相关资源
      最近更新 更多