asp.net - 将 ADO.NET 实体数据模型添加到 Azure 函数项目会导致异常

标签 asp.net entity-framework azure azure-functions

我正在将 webjob(控制台应用程序)转换为 azure 函数: 因此,azure函数的目标框架是.Net Framework 4.6.1,我从NuGet数据包管理器安装 Entity Framework 6.0.0,当我添加新项目(ADO.Net实体数据模型)并配置我得到的连接字符串时以下错误:“调用目标已引发异常”,以及日志:

=====================
10/8/2018 4:18:03 PM
Recoverable
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->     Microsoft.VisualStudio.ProjectSystem.ProjectException: The item type 'EntityDeploy' is not supported by this project item provider.
at Microsoft.VisualStudio.ProjectSystem.ProjectErrorUtilities.ThrowProjectExceptionHelper(Exception innerException, String unformattedMessage, Object[] args)
at Microsoft.VisualStudio.ProjectSystem.ProjectErrorUtilities.ThrowProjectException(String message, Object arg0)
at Microsoft.VisualStudio.ProjectSystem.CpsProjectItem`1.<SetItemTypeAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.PropertyPages.PageItemTypeProperty.<SetValueAsStringAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.DynamicTypeBrowseObject.<>c__DisplayClass112_0.<<VSLangProj80-FileProperties2-set_ItemType>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<>c__DisplayClass569_0.<<Mutate>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
at Microsoft.VisualStudio.ProjectSystem.ProjectMultiThreadedService.ExecuteSynchronously(Func`1 asyncAction)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.Mutate(Func`1 asyncAction, Boolean includesFileSystemChange, String telemetryActivity)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.PropertyPages.DynamicTypeBrowseObject.VSLangProj80.FileProperties2.set_ItemType(String value)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.VisualStudio.Telemetry.WindowsErrorReporting.WatsonReport.GetClrWatsonExceptionInfo(Exception exceptionObject)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAPropertyInfo.set_Value(Object value)
at Microsoft.Data.Entity.Design.VisualStudio.ModelWizard.ModelObjectItemWizard.RunFinished()
at Microsoft.VisualStudio.TemplateWizard.Wizard.Execute(Object application, Int32 hwndOwner, Object[]& ContextParams, Object[]& CustomParams, wizardResult& retval)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(Int32 hr, Int32[] expectedHRFailure)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.RunWizard(SimpleHierarchyNode parentNode, String itemName, String wizardToRun, IntPtr dlgOwner)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<AddItemWithSpecificAsync>d__613.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<>c__DisplayClass476_0.<<AddItem>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
at Microsoft.VisualStudio.Threading.JoinableTask`1.CompleteOnCurrentThread()
at Microsoft.VisualStudio.ProjectSystem.ProjectMultiThreadedService.ExecuteSynchronously[T](Func`1 asyncAction)
at Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.ProjectNode.<>c__DisplayClass568_0.<HrInvoke>b__0()
at Microsoft.VisualStudio.ProjectSystem.VS.HResult.Invoke(Func`1 action, IServiceProvider vsShellServiceProvider, IProjectFaultHandlerService projectFaultHandlerService, UnconfiguredProject project)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
t Microsoft.VisualStudio.ProjectSystem.CommonProjectSystemTools.Rethrow(Exception ex)
at Microsoft.VisualStudio.ProjectSystem.ProjectErrorReporting.<>c__DisplayClass6_0.<SubmitErrorReport>b__0()
at Microsoft.VisualStudio.ProjectSystem.ExceptionFilter.<>c__DisplayClass2_0.<Guard>g__action|0()
at GuardMethodClass.GuardMethod(Func`1 , Func`2 , Func`2 )
===================

我尝试恢复到 EntityFramework 5,但得到了相同的结果。

我还尝试将 edm 添加到单独的文件夹中。

仅生成 .edmx 、 .designer.cs 和 .edmx.diagram 文件;缺少的文件:.context.tt、.context.cs 和 .tt。

谁知道可能导致此问题的原因是什么?

单独说明

我尝试查看问题是否是由于使用4.6.1框架引起的,并使用4.6.1框架创建了一个控制台应用程序,并且我能够正常添加数据库。

附加信息!

问题可能与 Entity Framework 6 相关,因为在尝试构建时,我在 Model.Designer.csSystem.Data.Entity 命名空间中遇到错误。手动将所有 System.Data.Entity 更改为 System.Data.Entity.Core 可以消除错误,但是每当我从 Model.edmx 中的数据库更新模型时code> 代码被重新生成...

最佳答案

感谢this天才我发现在azure函数项目中添加数据库时,Visual Studio没有正确生成所有文件;这就是为什么我必须添加 .NET Framework 类库作为第二个项目。在其中添加 ADO .NET 实体数据模型,然后只需在 local.settings.json 中添加其连接字符串即可。

"ConnectionStrings": {
  "Entities": {
    "ConnectionString": "[…]",
    "ProviderName": "System.Data.EntityCLient"
  }
}

提示您必须将连接字符串中的"替换为'

另外,不要忘记将第二个类库项目添加为第一个类库项目的依赖项!

希望有帮助!!

注意

这是针对azure函数v1的,我使用的是azure函数v2,那么你必须使用 Entity Framework 核心:/

关于asp.net - 将 ADO.NET 实体数据模型添加到 Azure 函数项目会导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52703903/

相关文章:

asp.net - Azure 网站 WebResource.axd 返回 404 错误或 500 错误

c# - 将字符串解析为字节数组 C#

entity-framework - 什么会导致 Entity Framework 在现有数据上保存已卸载(但可延迟加载)的引用?

entity-framework - 不支持关键字 : 'metadata' error in Azure function

azure - 使用 Terraform 分配 Synapse 角色时出错

Azure Bicep 无法为 Azure Function App 创建托管计划

.net - ASP.NET MVC 3 动态控件

如果 WSDL 不可用或无效,C# Web 服务客户端将失败

c# - 在 Docker 中使用 EF Core 对 SQLite DB 的每个查询都非常慢,除非有对 OpenConnection 的显式调用

windows - 通过ARM模板在Azure Windows上运行powershell命令