C# .NET Core上Azure Function v2上的EF Core迁移出现问题
我有一个名为C# .NET Core上Azure Function v2上的EF Core迁移出现问题,c#,visual-studio,entity-framework,.net-core,azure-functions,C#,Visual Studio,Entity Framework,.net Core,Azure Functions,我有一个名为MyApp的项目,其中包含模型。为了从这些模型创建初始数据库,我在Visual Studio的包管理器控制台中使用了以下命令: dotnet ef迁移-v添加初始创建 我的项目的目录结构如下所示: MyApp |-bin | |-Debug | | |-netcoreapp2.1 | | | |-bin . . . | |-... . . . | |-MyApp.dll . . . | |-... |-
MyApp
的项目,其中包含模型。为了从这些模型创建初始数据库,我在Visual Studio的包管理器控制台中使用了以下命令:
dotnet ef迁移-v添加初始创建
我的项目的目录结构如下所示:
MyApp
|-bin
| |-Debug
| | |-netcoreapp2.1
| | | |-bin
. . . | |-...
. . . | |-MyApp.dll
. . . | |-...
|-MyApp.deps.json
|-MyApp.runtimeconfig.dev.json
|-MyApp.runtimeconfig.json
问题
当我运行上述命令创建初始数据库时,会弹出以下错误:
An assembly specified in the application dependencies manifest
(MyApp.deps.json) was not found:
package: 'MyApp', version: '1.0.0'
path: 'MyApp.dll'
因此,命令找不到bin
目录中的MyApp.dll
文件
从以下详细输出中可以清楚地看到这一点:
PM> dotnet ef migrations -v add InitialCreate
Using project 'C:\Users\rohan\Projects\MySolution\myproject\MyApp.csproj'.
Using startup project 'C:\Users\rohan\Projects\MySolution\myproject\MyApp.csproj'.
Writing 'C:\Users\rohan\Projects\MySolution\myproject\obj\MyApp.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\rohan\AppData\Local\Temp\tmp4BF.tmp /verbosity:quiet /nologo C:\Users\rohan\Projects\MySolution\myproject\MyApp.csproj
Writing 'C:\Users\rohan\Projects\MySolution\myproject\obj\MyApp.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\rohan\AppData\Local\Temp\tmpEB2.tmp /verbosity:quiet /nologo C:\Users\rohan\Projects\MySolution\myproject\MyApp.csproj
dotnet build C:\Users\rohan\Projects\MySolution\myproject\MyApp.csproj /verbosity:quiet /nologo
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:17.49
dotnet exec --depsfile C:\Users\rohan\Projects\MySolution\myproject\bin\Debug\netcoreapp2.1\MyApp.deps.json
--additionalprobingpath C:\Users\rohan\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder"
--runtimeconfig C:\Users\rohan\Projects\MySolution\myproject\bin\Debug\netcoreapp2.1\MyApp.runtimeconfig.json
"C:\Program Files\dotnet\sdk\2.1.700\DotnetTools\dotnet-ef\2.1.11\tools\netcoreapp2.1\any\tools\netcoreapp2.0\any\ef.dll"
migrations add InitialCreate
--assembly C:\Users\rohan\Projects\MySolution\myproject\bin\Debug\netcoreapp2.1\MyApp.dll
--startup-assembly C:\Users\rohan\Projects\MySolution\myproject\bin\Debug\netcoreapp2.1\MyApp.dll
--project-dir C:\Users\rohan\Projects\MySolution\myproject\ --language C#
--working-dir C:\Users\rohan\Projects\MySolution\myproject --verbose --root-namespace MyApp
dotnet : Error:
At line:1 char:1
+ dotnet ef migrations -v add InitialCreate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Error::String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
An assembly specified in the application dependencies manifest (MyApp.deps.json) was not found:
package: 'MyApp', version: '1.0.0'
path: 'MyApp.dll'
基本上,在--assembly
和--startup assembly
选项中的dll
文件路径不正确。这是因为(通过上述命令)生成的MyApp.deps.json包含以下内容:
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v2.1",
"signature": "..."
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v2.1": {
"MyApp/1.0.0": {
"dependencies": {
...
},
"runtime": {
"MyApp.dll": {}
}
},
...
}
...
}
...
}
这里,MyApp/1.0.0
中的runtime
JSON对象包含dll
文件的相对路径,即MyApp.dll
,而应该是bin/MyApp.dll
我应该怎么做才能在
运行时JSON对象中写入正确的路径?我遇到了一个非常类似的问题。经过数小时的搜索和实验,我发现了以下帖子:
将数据库内容分离到一个单独的类库项目中,然后添加IDesignTimeDbContextFactory为我解决了这个问题
祝你好运经过我自己的大量研究,我在这里找到了这项工作:
但需要将其调整为n层设置,从而使模型位于另一个项目(以及其他项目)中。我需要复制所有以“Crank”(功能代码名)开头的dll,而不仅仅是启动项目的dll
最终解决方案是将以下内容添加到启动csproj中
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="copy /Y "$(TargetDir)bin\Crank*.dll" "$(TargetDir)Crank*.dll"" /></Target>
据我所知,这将复制项目DLL,以便EF add迁移和更新数据库可以使用它们
请注意,在运行添加迁移或更新数据库之前,还需要设置环境变量的步骤:
$env:CrankDealershipDatabase=“服务器=。\ss20161433;数据库=CrankDealership\u开发;受信任的\u连接=真;MultipleActiveResultSets=真”
这些是当时的包裹:
Microsoft.Azure.Functions.Extensions 1.0.0
Microsoft.EntityFrameworkCore.SqlServer 2.2.6
Microsoft.NET.Sdk.Functions 1.0.29
Microsoft.EntityFrameworkCore 2.2.6
Microsoft.EntityFrameworkCore.Design 2.2.6
Microsoft.EntityFrameworkCore.Tools 2.2.6
Microsoft.VisualStudio.Web.CodeGeneration.Design 2.2.3你在1 azure功能中加入了这么多逻辑?没有。你为什么这么认为?我的azure函数非常小。因为你需要一个完整的orm来管理你的数据库调用。我本可以使用SqlClient,但我认为orm会使这项工作更容易。是的,除非你手动编写sql并使用ADO.NET,否则你并不酷。为什么要使用一个使用反射的工具来为你做这件事呢。另外,使用记事本。jfc。