无法 运行 在 运行 时设置 DbContext 连接字符串的 EF 迁移并将项目与应用程序分开
Can't run EF migration where DbContext connection string is set at runtime and separate project from application
我有一个 class 库项目,其中有一个 DbContext class,如下所示:
public DbContext() : base(ContextInitializer.GetConnectionStringName())
{
以便用户(其他程序员)可以在他们的应用程序启动时设置它,当他们可以在他们在任何项目中选择的任何数据库上使用我的 API 和实体时,然后设置连接字符串。现在我正尝试在我的 class 库中生成一个迁移配置。我试过:
Enable-Migrations -ProjectName "ClassLibraryProject" -ContextTypeName "MyDbContext" -StartUpProjectName "MyWebApp" -ConnectionString "MyConnectionString" -ConnectionProviderName "System.Data.SqlClient"
根据此处遇到相同错误的其他答案,但没有帮助。我仍然得到:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:718 char:5
+ $domain.SetData('project', $project)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject'
in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation,
Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is
not marked as serializable." At
C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:719
char:5
+ $domain.SetData('contextProject', $contextProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject'
in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation,
Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is
not marked as serializable." At
C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:720
char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
我在这里看到的其他答案说使用 -StartUpProjectName 参数,我这样做了,但仍然出现错误。我认为这是因为我的项目位于我正在使用的解决方案中的嵌套文件夹中,但是提供项目名称 "path/ProjectName" 触发了找不到项目的错误,所以不可能。
编辑:我发现了这个:EF 6 with a dnx project,我的项目是 Asp.NET Core class 库,目标是 461 运行time 并在 Asp.NET Core 中使用网络应用程序。因此,无法使用带有 ASP.NET Core 的 EF 6 运行 在 class 库中进行迁移?
确保 MyWebApp 有一个带有名为 MyConnectionString
的连接字符串的 web.config。
Enable-Migrations
-ProjectName "ClassLibraryProject"
-ContextTypeName "MyDbContext"
-StartUpProjectName "MyWebApp"
-ConnectionString "MyConnectionString"
-ConnectionProviderName "System.Data.SqlClient"
好吧,经过多方查找,您不能默认执行此操作。自 1 月以来,class 库根本没有工具,这对我来说似乎很糟糕。并且没有工具作为 link 我发布了针对使用 EF6 的 461 的 .NET CORE class 库的提及,因为 EF6 工具无法识别 project.json 依赖格式。
然而,幸运的是,一位名叫 Mohammad Rahhal 的先生创建了这样一个库来完成此任务:https://github.com/mrahhal/Migrator.EF6/blob/master/README.md
并使用本期描述的信息:https://github.com/mrahhal/Migrator.EF6/issues/9
我能够成功 运行 迁移,它确实需要一些 hacky 的东西,但它暂时有效,比其他地方提供的其他替代方案更好。
1) 为 Migrator 下载这个 nuget 包。EF6.Tools nuget。
2) 更改 project.json 以包括:
{
"version": "1.0.0-*",
"dependencies": {
"EntityFramework": "6.1.3",
"Migrator.EF6.Tools": "1.0.5"
},
"frameworks": {
"net461": {}
},
"buildOptions": {
"emitEntryPoint": true
},
"tools": {
"Migrator.EF6.Tools": {
"imports": "portable-net45+win8+dnxcore50",
"version": "1.0.5"
}
}
}
3) 将带有主存根的 program.cs 文件添加到 class 库项目:
public class Program
{
public static void Main(string[] args)
{
}
}
您现在已设置为 运行 从 VS2015 Dev 命令提示符迁移。导航到项目目录,然后 运行 上面的自述文件 link 中描述的迁移命令。
但是,应该注意的是,完成 运行 迁移后,再次将 emitEntryPoint
值设置为 false,这样它仍然可以像普通的 class 库一样处理。基本上你是在欺骗 EF 工具认为你的 class 库是一个控制台应用程序,但你不希望它像部署时那样被对待。
我正在使用 Visual Studio 2017
在我能够成功 运行 迁移的地方,它确实需要
在 DbContext 所在的 class 库中安装以下 NuGet 包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.工具
我今天早上遇到了同样的问题,我将 EF nuget 包更新到 Entity Framework 6.3.0。这很有帮助。
我有一个 class 库项目,其中有一个 DbContext class,如下所示:
public DbContext() : base(ContextInitializer.GetConnectionStringName())
{
以便用户(其他程序员)可以在他们的应用程序启动时设置它,当他们可以在他们在任何项目中选择的任何数据库上使用我的 API 和实体时,然后设置连接字符串。现在我正尝试在我的 class 库中生成一个迁移配置。我试过:
Enable-Migrations -ProjectName "ClassLibraryProject" -ContextTypeName "MyDbContext" -StartUpProjectName "MyWebApp" -ConnectionString "MyConnectionString" -ConnectionProviderName "System.Data.SqlClient"
根据此处遇到相同错误的其他答案,但没有帮助。我仍然得到:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:718 char:5 + $domain.SetData('project', $project) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:719 char:5 + $domain.SetData('contextProject', $contextProject) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\me.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:720 char:5 + $domain.SetData('startUpProject', $startUpProject) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException
我在这里看到的其他答案说使用 -StartUpProjectName 参数,我这样做了,但仍然出现错误。我认为这是因为我的项目位于我正在使用的解决方案中的嵌套文件夹中,但是提供项目名称 "path/ProjectName" 触发了找不到项目的错误,所以不可能。
编辑:我发现了这个:EF 6 with a dnx project,我的项目是 Asp.NET Core class 库,目标是 461 运行time 并在 Asp.NET Core 中使用网络应用程序。因此,无法使用带有 ASP.NET Core 的 EF 6 运行 在 class 库中进行迁移?
确保 MyWebApp 有一个带有名为 MyConnectionString
的连接字符串的 web.config。
Enable-Migrations
-ProjectName "ClassLibraryProject"
-ContextTypeName "MyDbContext"
-StartUpProjectName "MyWebApp"
-ConnectionString "MyConnectionString"
-ConnectionProviderName "System.Data.SqlClient"
好吧,经过多方查找,您不能默认执行此操作。自 1 月以来,class 库根本没有工具,这对我来说似乎很糟糕。并且没有工具作为 link 我发布了针对使用 EF6 的 461 的 .NET CORE class 库的提及,因为 EF6 工具无法识别 project.json 依赖格式。
然而,幸运的是,一位名叫 Mohammad Rahhal 的先生创建了这样一个库来完成此任务:https://github.com/mrahhal/Migrator.EF6/blob/master/README.md
并使用本期描述的信息:https://github.com/mrahhal/Migrator.EF6/issues/9
我能够成功 运行 迁移,它确实需要一些 hacky 的东西,但它暂时有效,比其他地方提供的其他替代方案更好。
1) 为 Migrator 下载这个 nuget 包。EF6.Tools nuget。
2) 更改 project.json 以包括:
{
"version": "1.0.0-*",
"dependencies": {
"EntityFramework": "6.1.3",
"Migrator.EF6.Tools": "1.0.5"
},
"frameworks": {
"net461": {}
},
"buildOptions": {
"emitEntryPoint": true
},
"tools": {
"Migrator.EF6.Tools": {
"imports": "portable-net45+win8+dnxcore50",
"version": "1.0.5"
}
}
}
3) 将带有主存根的 program.cs 文件添加到 class 库项目:
public class Program
{
public static void Main(string[] args)
{
}
}
您现在已设置为 运行 从 VS2015 Dev 命令提示符迁移。导航到项目目录,然后 运行 上面的自述文件 link 中描述的迁移命令。
但是,应该注意的是,完成 运行 迁移后,再次将 emitEntryPoint
值设置为 false,这样它仍然可以像普通的 class 库一样处理。基本上你是在欺骗 EF 工具认为你的 class 库是一个控制台应用程序,但你不希望它像部署时那样被对待。
我正在使用 Visual Studio 2017
在我能够成功 运行 迁移的地方,它确实需要
在 DbContext 所在的 class 库中安装以下 NuGet 包
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.工具
我今天早上遇到了同样的问题,我将 EF nuget 包更新到 Entity Framework 6.3.0。这很有帮助。