Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 首先为EF 6代码应用迁移-未找到上下文类型_C#_Entity Framework 6_Ef Code First_.net 4.8 - Fatal编程技术网

C# 首先为EF 6代码应用迁移-未找到上下文类型

C# 首先为EF 6代码应用迁移-未找到上下文类型,c#,entity-framework-6,ef-code-first,.net-4.8,C#,Entity Framework 6,Ef Code First,.net 4.8,我正在从事WCF.NETFramework4.8项目,其WCF在控制台应用程序中自托管 我已经在WCF控制台应用程序引用的单独项目中设置了定义的POCO类和DbContext 我已经在主控制台应用程序的app.config中声明了DbContext的连接字符串 当我尝试执行EF Code First命令时,如EnableMigrations和Add Migration它们无法执行,错误如下: 在程序集'MAL.App.ConsoleHost'中未找到上下文类型。 控制台应用程序App.Conf

我正在从事WCF.NETFramework4.8项目,其WCF在控制台应用程序中自托管

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

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

当我尝试执行EF Code First命令时,如EnableMigrationsAdd Migration它们无法执行,错误如下:

在程序集'MAL.App.ConsoleHost'中未找到上下文类型。

控制台应用程序App.Config 控制台主程序
如果在主运行时的单独项目中定义了DbContext,并且将主运行时设置为解决方案中的默认项目,则会发生此错误

首次打开Package Manager控制台时,它假定解决方案默认项目是您要对其执行命令的项目,如
启用迁移

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

不过这里有一个问题,仍然将您的默认项目保留在解决方案中作为您的控制台应用程序

  • 解决方案默认项目不仅仅是当您点击F5运行时将执行的项目,而是VisualStudio需要在运行时访问app/web配置文件时随时使用的项目
  • 这与使用EDMX设计图面的早期版本的EF相同,甚至在使用数据集设计器之前,默认项目配置文件中的连接字符串就是VS在设计时使用的字符串
您还可以对大多数EF CLI命令使用
-ProjectName
参数来传递项目名称,而不是使用默认值。为简洁起见,我通常将其保留在有多个项目具有上下文的场景中,尤其是在同一项目中有多个上下文的场景中。您不希望每次希望
添加迁移
更新数据库
或任何其他类似的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> 
PM>启用迁移-?
名称
启用迁移
提要
在项目中启用代码优先迁移。
语法
启用迁移[-ContextTypeName][-EnableAutomaticMigrations][-MigrationDirectory][-ProjectName][-StartUpProjectName][-ContextProjectName][-ConnectionString名称]
[-Force][-ContextAssemblyName][-AppDomainBaseDirectory][]
启用迁移[-ContextTypeName][-EnableAutomaticMigrations][-MigrationDirectory][-ProjectName][-StartUpProjectName][-ContextProjectName]-连接字符串
-ConnectionProviderName[-Force][-ContextAssemblyName][-AppDomainBaseDirectory][]
描述
通过在项目中构建迁移配置类来启用迁移。如果
目标数据库是由初始值设定项创建的,将创建初始迁移(除非
自动迁移是通过EnableAutomaticMigrations参数启用的。
相关链接
评论
要查看示例,请键入:“get help Enable Migrations-examples”。
有关详细信息,请键入:“get help Enable Migrations-detailed”。
有关技术信息,请键入:“get help Enable Migrations-full”。
PM>

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

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

- add-migration Initial -ProjectName MyContextProjectNameHere

-  update-database -ProjectName MyContextProjectNameHere

嗨,Chris,我需要先运行迁移,然后才能使用MyDbContext,但我无法这样做。我相信控制台应用程序,在这个项目中没有任何web前端。我在上面的问题中添加了错误的屏幕截图,找到了答案。请参考上面的内容这也很有效,但每次都需要键入很多内容,只需在package manager控制台中设置默认项目即可简化在命令行上的交互这是一个没有详细说明的内容,主要是因为大多数示例都简化为单个项目,我建议在不同的项目中使用EF上下文和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();
}
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> 
- Enable-Migrations -ProjectName MyContextProjectNameHere -StartUpProjectName MyStartUpProjectNameHere -Verbose

- add-migration Initial -ProjectName MyContextProjectNameHere

-  update-database -ProjectName MyContextProjectNameHere