MVC4 API on Azure WebSites or Azure Cloud Services [Error] : 'System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption'

孤街浪徒 提交于 2019-12-18 05:57:25

问题


I am getting this error when I try to deploy my Web API project to both Cloud WebSites and Cloud Hosted Services on Azure:

"{"ExceptionType":"System.TypeLoadException","Message":"Could not load type 'System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.","StackTrace":"\r\nServer stack trace: \r\n   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)\r\n   at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n   at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n   at System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache()\r\n   at System.Lazy`1.CreateValue()\r\n\r\nException rethrown at [0]: \r\n   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)\r\n   at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n   at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n   at System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache()\r\n   at System.Lazy`1.CreateValue()\r\n   at System.Lazy`1.LazyInitValue()\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()\r\n   at System.Lazy`1.CreateValue()\r\n\r\nException rethrown at [1]: \r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"}"

Here is just the message:

"Could not load type 'System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."

It happens when I try to hit any of my API's from the remote site. I know its not my 500 errors because some of my API's wont throw them regardless of what happens.

One thing to note and I am not sure if this would affect anything is I started this project targeted at .NET 4.5 but then switched it to 4.0 when Azure Web Sites said they don't support 4.5.

I can't find anything about this except this link: http://support.appharbor.com/discussions/problems/4822-internal-server-error-500-mvc-4-web-api

except that guy is using app-harbor, andeven still there is still no solution. Thanks of any help.


回答1:


You are right about starting in .NET 4.5 and rolling back causing the issue, here is the official documentation about this issue and a link.

Changing an ASP.NET MVC 4 project to target 4.0 from 4.5 does not update the EntityFramework assembly reference: If you change an ASP.NET MVC 4 project to target 4.0 after targetting 4.5 the reference to the EntityFramwork assembly will still point to the 4.5 version. To fix this issue reinstall the EntityFramework NuGet package.

Reinstalling EntityFramework after switching project to .NET 4.0 did the trick.

Reference: http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253815

This happened to me when I compiled an MVC RC project in .NET 4.0 and tried to host it in IIS on a server that did not have Visual Studio 2012 RC installed.

(Note: Installing Visual Studio 2012 RC on the server also fixed the issue, but for backward-compatibility the above fix is much better.)




回答2:


So it seemed that the problem was caused by me rolling back to .NET 4 from 4.5 and Entity not rolling back. It seems that Entity 5 only runs on .NET 4.5 and when the deployment was expecting 4.3.1 it had a 5.0.0 DLL and it messed everything up.



来源:https://stackoverflow.com/questions/11024681/mvc4-api-on-azure-websites-or-azure-cloud-services-error-system-componentmo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!