首页 > 解决方案 > Can't add EF 6.4 migrations in a .Net Standard 2.1 project

问题描述

We're in the final stages of porting a huge solution over to .Net Core (from 4.7.2), but we're not yet ready to migration to Entity Framework Core, as we make use of table-per-type hierarchies, etc. We use EF code-first. So, the project containing our DbContext and EF migrations is now running .netstandard2.1 and references EF 6.4 (which, as of v6.3 apparently supports .net core). I understand that the EF tooling changed from v6.2 -> v6.4, so our traditional way of creating an EF migration now results in the following error in the Package Manager Console:

add-migration TEST -project Data

Project 'Data' targets framework '.NETStandard'. The Entity Framework Package Manager Console Tools don't support this framework.

After some investigation, I came across this, which suggests that you need to use a .Net Core assembly as a dummy start-up project. After creating a .netcore 3.1 console app project (called Data_Startup, which references the Data project) and modifying my command slightly, I now get the following error:

dotnet ef migrations add TEST --project Data --startup-project Data_Startup -c CustomContext

No DbContext named 'CustomContext' was found.

This seems like it's at least attempting the migration, but I'm now stuck. I've tried fully qualifying the DbContext with the namespace and adding the DBContext class as a "linked file" to the new dummy project, but I'm faced with the same error.

I've also attempted to use the EF 6.4 tool: dotnet C:\Users\xxxx.nuget\packages\entityframework\6.4.0\tools\netcoreapp3.0\any\ef6.dll" migrations add TEST --assembly Data

Your target project 'Data' doesn't reference EntityFramework. This package is required for the Entity Framework Core Tools to work. Ensure your target project is correct, install the package, and try again.

...despite it definitely being installed in the Data project!

What am I doing wrong? Which tool do I actually need to use? Is this project configuration even possible?

标签: c#entity-frameworkentity-framework-6entity-framework-migrations

解决方案


我知道这是一篇较旧的帖子,但是在遇到同样的问题时我偶然发现了它,所以希望我的解决方案能对某人有所帮助。如果您有 .NET 5 或 .NET Core 项目并在其中使用 EF6,以下是从 PowerShell 运行迁移的方法:

添加迁移:

cd "your_solution_directory"
$assemblyDirectory = "Test.Project\bin\Debug\net5.0-windows"

& dotnet exec --depsfile "$assemblyDirectory\Test.Project.deps.json" --runtimeconfig "$assemblyDirectory\Test.Project.runtimeconfig.json" packages\EntityFramework.6.4.4\tools\netcoreapp3.0\any\ef6.dll migrations add TestMigrationName --json --verbose --no-color --prefix-output --assembly "$assemblyDirectory\Test.Project.dll" --project-dir Test.Project\ --root-namespace Test.Project --connection-string "Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=DBNAME;Integrated Security=True" --connection-provider "System.Data.SqlClient"

运行迁移:

cd "your_solution_directory"
$assemblyDirectory = "Test.Project\bin\Debug\net5.0-windows"

& dotnet exec --depsfile "$assemblyDirectory\Test.Project.deps.json" --runtimeconfig "$assemblyDirectory\Test.Project.runtimeconfig.json" packages\EntityFramework.6.4.4\tools\netcoreapp3.0\any\ef6.dll database update --target TARGET_MIGRATION_NAME_HERE --verbose --no-color --prefix-output --assembly "$assemblyDirectory\Test.Project.dll" --project-dir Test.Project\ --language C# --root-namespace Test.Project --connection-string "Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=DBNAME;Integrated Security=True" --connection-provider "System.Data.SqlClient"

由于像在 .NET Framework 中那样直接使用 ef6.exe 不起作用,因此这些长命令似乎是唯一的方法。如果您仍然遇到问题并且我的解决方案没有帮助您,请尝试从包管理器控制台运行 Update-Database 或 Add-Migration 命令并添加 --verbose 标志。这将向您显示 Visual Studio 正在运行的确切 PowerShell 命令,您可以在脚本中重复使用这些命令。

此外,您可以让它读取您的 App.Config 文件,但由于某种原因, --configuration 参数被忽略。它总是使用需要存在于程序集目录中的 ef6.dll.config 文件。您可以将构建配置设置为始终将 App.config 文件复制到 ef6.dll.config


推荐阅读