当前位置: 首页 > 知识库问答 >
问题:

首先应用EF 6代码的迁移-找不到上下文类型

林鸿飞
2023-03-14

我在WCF.NET Framework 4.8项目上工作,它的WCF是在控制台应用程序中自宿主的。

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

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

当我尝试执行EF Code First命令(如enablemigrationsadd-migrations)时,它们无法执行,出现以下错误:

程序集“Mal.App.ConsoleHost”中找不到上下文类型

<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; }
}
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();
}

共有2个答案

柯易安
2023-03-14

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

- Enable-Migrations -ProjectName MyContextProjectNameHere -StartUpProjectName MyStartUpProjectNameHere -Verbose

- add-migration Initial -ProjectName MyContextProjectNameHere

-  update-database -ProjectName MyContextProjectNameHere
楚博雅
2023-03-14

当您的DbContext是在一个单独的项目中定义到您的主运行库,并且您的主运行库被设置为解决方案中的默认项目时,会发生此错误。

当第一次打开包管理器控制台时,它假定解决方案默认项目是您要对其执行命令的项目,如enable-migrations

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

但关键是,仍然将解决方案中的默认项目保留为控制台应用程序。

  • 解决方案默认项目不仅仅是在单击F5以运行时执行的项目,这是Visual Studio在运行时需要访问APP/Web配置文件时使用的项目。
  • 这对于使用EDMX设计图面的EF的早期版本是相同的,甚至在此之前,使用数据集设计器时,来自默认项目配置文件的连接字符串就是VS在设计时使用的连接字符串。

您还可以对大多数EF CLI命令使用-projectname参数来传递项目的名称,而不是使用默认值。为简洁起见,我通常将此保留在有多个项目具有上下文的场景中,或者特别是在同一个项目中有多个上下文的情况下。您不希望每次想要add-migrationupdatabase或任何其他类似的scaffold命令时都必须键入项目名称。

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

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> 
 类似资料:
  • 我部署Laravel准骨项目到Microsoft Azure,但每当我试图执行我得到的错误: [2015-06-13 14:34:05]生产。错误:异常'Symfony\Component\Debug\Exc0019\FatalErrorExc0019',消息'Class"未找到'在D:\home\site\供应商\laravel\框架\src\Illumate\数据库\Migrations\Mi

  • (至少我可以只用名字,跳过时间戳……) 有更简单的方法吗?

  • 我们正在Karaf中部署我们的应用程序,并试图使用Flyway来处理数据库迁移,但它在运行时找不到我们的文件。我检查过的东西: > 当我为Flyway启用调试时,我可以看到它在类路径上找到了文件夹,但其中没有文件。 当408与安装的捆绑包匹配时,日志显示它正在查找正确的捆绑包。 我不确定还要查找什么,因为没有记录任何错误,而且它似乎正在查找它应该位于的文件夹,而不是sql文件。我假设这是一个清单类

  • servlet-context.xml 应用程序上下文. xml 调用方法 堆栈跟踪

  • 我在我支持的许多应用程序中实现了Flyway,它像梦一样工作。 但是,当我将应用程序部署到测试环境时,迁移就停止了。 经过一些调查,我发现当迁移被加载到jar文件中时,它们不是通过Flyway定位的,而是当它们没有被压缩时(比如当我在Eclipse中工作时,或者如果我将jar解压到类路径中),它会按预期工作。 由于应用程序的插件架构,我无法使用“默认”设置,因此我将Flyway对象设置为: 如果要

  • Pillow 是对 PIL 的功能增加,想要在 Pillow 下运行 PIL 的代码,只需要: 把这个: import Image 修改成: from PIL import Image 注意,:py:mod:`_imaging` 模块已经被移除,现在可以这样导入:: from PIL.Image import core as _imaging 另外,图像插件导入机制已经改变。Pi

  • 我在我的项目中设置了具有以下目录布局的Flyway(不包括不相关的目录): 和build.xml的相关部分(flyway.url和flyway.driver设置在不包括在这里的属性文件中---Flyway与数据库交谈没有问题): 以下是在编译目标中创建的迁移JAR的布局: 下面是来自的输出: Flyway没有发现问题。类文件。我做错了什么? 编辑:如果有人知道使用Ant(没有Maven,没有常春藤