Asp.net-Mvc-3

此版本的 SQL Server 不支持沒有聚集索引的表

  • November 21, 2012

我正在使用SQL Server 數據庫開發****vs 2010 和 EF 4.1。下面提到的程式碼適用於本地 SQL 伺服器 DB。(SQL 2008)。

但是當我發布適用於Windows AZURE 雲SQL Azure的 MVC 應用程序時,它給出了下面提到的錯誤

  1. 為什麼此錯誤僅返回 SQL Azure(使用桌面 SQL Server 2008)?
  2. 如何擺脫這個?

我的儲存庫**程式碼範例如下。呼叫****Catalog.SaveChanges()**方法時出現下面提到的錯誤 。

using (var catalog = new DataCatalog())
{
   var retailSaleReturn = new RetailSaleReturn
   {
       ReturnQuantity = returnQuantity,
       Product = saleDetailObj.Product,
       Owner = owner,
       Provider = provider,
   };

   //add to context
   Catalog.RetailSaleReturns.Add(retailSaleReturn);

   //save for db
   Catalog.SaveChanges();
}

DbUpdateException如下所示:

{"An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details."}

InnerException如下所示:

{"Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again."}

StackTrace如下所示

at System.Data.Entity.Internal.InternalContext.SaveChanges()
  at PawLoyalty.Data.Repositories.CustomersRepository.ReturnRetailOnlySales(Guid saleDetailId, Int32 returnQuantity, String providerKey, String ownerKey) in D:\PawLoyalty Module\PawLoyalty\PawLoyalty\PawLoyalty.Data\Repositories\CustomersRepository.cs:line 550
  at PawLoyalty.Web.Areas.Providers.Controllers.CustomersController.ReturnRetailOnlySales(String providerKey, String ownerKey, String petKey, Guid saleDetailId, Int32 returnQuantity) in D:\PawLoyalty Module\PawLoyalty\PawLoyalty\PawLoyalty.Web\Areas\Providers\Controllers\CustomersController.cs:line 942
  at lambda_method(Closure , ControllerBase , Object[] )
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.<c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

您需要在SQL Azure中要添加行的所有表上創建聚集索引;否則插入語句總是失敗。

CREATE UNIQUE CLUSTERED INDEX Idx_TableName ON TableName(yourGUIDColumn);

以下是針對這些索引的一般準則和限制的參考:MSDN Link

這是另一篇文章,解釋了這背後的原因:link

引用自:https://stackoverflow.com/questions/13492175