2012-11-01 149 views
57

我試圖在ASP.NET MVC 4中創建一個webapi。使用了實體框架5的webapi空間類型和我寫了一個非常簡單碼。在Azure上找不到'Microsoft.SqlServer.Types'版本10或更高版本

public List<Area> GetAllAreas() 
    { 
     List<Area> aList = db.Areas.ToList(); 
     return aList; 
    } 

Area包含DbGeometry。

當我運行這個地方它的工作原理,但是當我發佈到Azure它給了我這個錯誤:

Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.

任何人都知道如何解決這個問題? :)

謝謝!

+2

是否使用Azure網站或雲服務Web角色固定我的問題?另外,你的數據庫是一個SQL Azure數據庫嗎?您是否嘗試過針對SQL Azure數據庫運行本地代碼,並且這是否正常工作? –

回答

102

我找到了解決方案!只需安裝NuGet包Microsoft.SqlServer.Types

PM> Install-Package Microsoft.SqlServer.Types

Link for more info

+2

謝謝。這件事發生在凌晨2點後,就發生在我身上。 –

+3

很高興我把那個nuget包裝起來!它也總是讓我感動。 OMG! –

+0

OMG!它向Web應用程序添加了將近2MB的二進制數據,僅用於使用DbGeography(不,謝謝),在SQL Server中使用它時CPU也很重要......將其取出。 – Yovav

12

我也遇到了這個問題,但已經安裝了Microsoft.SqlServer.Types NuGet包。

什麼解決了我的問題是解決>參考> System.Data.Entity>屬性>複製本地,並將其設置爲True。

注: 複製本地的Microsoft.SqlServer.Types已經被設置爲true,即使該問題是與System.Data.Entity的,錯誤消息是仍有約Microsoft.SqlServer.Types。

該解決方案來自Windows Azure forum

+1

該選項在nuget包中不可用。 – Shimmy

2

的解決方案,我剛加入這行代碼的Global.asax.cs在Application_Start()

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin")); 

祝我的兄弟。

1

上述解決方案都不能解決我的問題。

  • 安裝SQL Server Feature Pack?是的
  • NuGet包安裝?是的
  • DLL存在於GAC和項目bin中?是

你知道嗎,這個錯誤也可能是由於服務器上的資源不足。我重新啓動了SQL Server並自動解析。

51

上述answer在可以使用程序集的版本11(SQL Server 2012)時正常工作。

我遇到了問題,因爲我的解決方案對同一程序集的版本13(SQL Server 2016)具有其他依賴關係。在這種情況下,請注意實體框架(至少v6.1.3)在其SqlTypesAssemblyLoader(此例外源)中進行了硬編碼,僅查找程序集的版本10和11。

若要解決此,我發現你可以告訴實體框架要使用像這樣在裝配:

SqlProviderServices.SqlServerTypesAssemblyName = 
    "Microsoft.SqlServer.Types, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"; 
+1

好點 - 同樣適用於只安裝SQL 2014 CLR類型的機器。在我們的例子中,我們剛剛安裝了SQL 2012 CLR類型並解決了問題;但如果您對組件的高版本有特定的依賴關係,那麼這看起來是最好的解決方案。 –

+0

該值應該在何時/何時設定? – Triynko

+1

這是一個靜態公共財產。它應該在應用程序啓動時設置。例如。我將其設置在我的Web應用程序的Global.asax.cs中的Application_Start事件中。 – Chris

0

剛剛有同樣的問題。我正在使用EF6並調用SQL,它具有使用空間命令的SQL函數。我通過單元測試來測試它,它工作正常。當我去到線了我Asp.Net的解決方案,我收到了錯誤

Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.

通過添加NUGET包「Microsoft.SqlServer.Types」和添加SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));Application_Start methodGlobal.asax.cs,一切運行良好。

14

有2種方法來解決這個問題:

  1. 如果您有服務器訪問,只需要安裝「微軟系統CLR類型的SQL Server 2012」 從https://www.microsoft.com/en-us/download/details.aspx?id=29065 或使用直接鏈接的下面 直接鏈接到X86:http://go.microsoft.com/fwlink/?LinkID=239643&clcid=0x409 ,或者直接鏈接到X64:http://go.microsoft.com/fwlink/?LinkID=239644&clcid=0x409
  2. 第二種方式是使用NuGet包管理器,安裝於

    安裝,包裝Microsoft.SqlServer.Types

然後按照該插件注意事項如下

To deploy an application that uses spatial data types to a machine that does not have 'System CLR Types for SQL Server' installed you also need to deploy the native assembly SqlServerSpatial110.dll. Both x86 (32 bit) and x64 (64 bit) versions of this assembly have been added to your project under the SqlServerTypes\x86 and SqlServerTypes\x64 subdirectories. The native assembly msvcr100.dll is also included in case the C++ runtime is not installed.

You need to add code to load the correct one of these assemblies at runtime (depending on the current architecture).

ASP.NET applications For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs:

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin")); 

Desktop applications For desktop applications, add the following line of code to run before any spatial operations are performed:

SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); 
+1

安裝SQL Server或NuGet沒有解決任何問題,這些簡單的CLR類型解決了這個問題。這應該是可以接受的解決方案。 –

+0

鏈接到X64對我有用 Microsoft SQL Server 2016(SP1)(KB3182545) - 13.0.4001.0(X64)Oct 28 2016 18:17:30 Copyright(c)Microsoft Corporation Express Edition(64-bit)on Windows 10 Enterprise 6.3 (Build 10586:) – zapoo

+0

我需要使用ASP.NET樣式加載器,但是我的開發路徑是'〜/'而不是'〜/ bin'。一定要檢查你的路徑。 – jocull

0

在我的情況下,嚴重組成連接字符串導致此。驗證您的連接字符串是否正確組成。

22

由於某種原因,我錯過了一個綁定重定向,它爲我解決了這個問題。

添加以下

<dependentAssembly> 
     <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" /> 
     <bindingRedirect oldVersion="10.0.0.0-11.0.0.0" newVersion="14.0.0.0" /> 
    </dependentAssembly> 
+0

爲了找出你的機器上的Microsoft.SqlServer.Types版本號是什麼,你可以使用'AppDomain currentDomain = AppDomain.CurrentDomain;程序集[] assems = currentDomain.GetAssemblies(); foreach(程序集中的程序集程序集) _logger.Info(assembly.GetName()。FullName); }'_logger是一個Nlog日誌記錄器 –

+0

這解決了我的問題(因爲類型已經安裝在我的情況)。如果有人在安裝SQL Server類型後仍然出現錯誤,請檢查此答案。 –

相關問題