2010-05-04 177 views
0

我正在嘗試執行一個存儲過程,該實體返回的數據與我在項目中使用的表實體的列完全相同。我在'添加功能導入'對話框中將'收集'屬性設置爲我的實體類型。實體框架與返回實體的'獲取'存儲過程

當執行存儲過程和進一步挖掘時,我得到一個NullReferenceException錯誤,似乎是因爲'EntityKey'屬性丟失。我能做些什麼來告訴它忽略實體的特殊屬性?

我已經有了'[ScaffoldColumn(false)]'的實體的部分類,但這似乎並不重要。

乾杯, 尼克

編輯:

這裏是堆棧跟蹤:

[NullReferenceException異常:未設置爲一個對象的實例對象引用] System.Data。 EntityKey.AddHashValue(Int32 hashCode,Object keyValue)+36 System.Data.EntityKey.GetHashCode()+7696654 System.Collections.Generic.GenericEqualityComparer 1.GetHashCode(T obj) +23 System.Collections.Generic.Dictionary 2。 FindEntry(TKEY的鍵)70 System.Collections.Generic.Dictionary 2.TryGetValue(TKey key, TValue& value) +17 System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry) +111 System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func 2 constructEntityDelegate,的EntityKey的EntityKey,EntitySet的EntitySet的)102 lambda_method(封閉,牛頭刨牀)460 System.Data.Common.Internal.Materialization.Coordinator 1.ReadNextElement(Shaper shaper) +170 System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +84 System.Collections.Generic.List 1 ..(IEnumerable 1 collection) +406 System.Linq.Enumerable.ToList(IEnumerable 1 source)+58 TWAgencySpend.Controllers.ReportsController.Advertising(Nullable 1 agencyId, Nullable 1 businessUnitId)in C:\ dasdsd \ Controllers \ ReportsController.cs:72 lambda_method(Closure,ControllerBase,Object [])+ 190 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object [] parameters)+51 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary 2 parameters) +409 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2參數)+52 System.Web.Mvc。 <> c__DisplayClassd.b__a()127 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter濾波器,ActionExecutingContext preContext,函數功能1 continuation) +436 System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1個濾波器,ActionDescriptor actionDescriptor,IDictionary的2 parameters) +305 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +830 System.Web.Mvc.Controller.ExecuteCore() +136 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +111 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +65 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44 System.Web.Mvc.Async.<>c__DisplayClass8 1.b__7(IAsyncResult的_)42 的System.Web。 Mvc.Async.WrappedAsyncResult`1.End()+141 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+54 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,對象標記)+ 40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult)+52 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的結果)+38 System.Web.CallHandlerExecutionStep。的System.Web .HttpApplication.IExecutionStep.Execute()+8836913 System.Web.HttpApplication。ExecuteStep(IExecutionStep一步,布爾& completedSynchronously)184

更新:這裏是該函數在SSDL項目:

<Function Name="GetAgencyReport" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"> 
     <Parameter Name="AgencyId" Type="int" Mode="In" /> 
     <Parameter Name="Year" Type="int" Mode="In" /> 
    </Function> 

,這裏是電話:

reportsViewModel.AgencyReportData = twAgencySpendEntities.GetAgencyReport(agencyId.Value, 2010).ToList(); 

這裏是我的實體的關鍵:

<EntityType Name="AdvertisingData"> 
    <Key> 
    <PropertyRef Name="AgencyId" /> 
    <PropertyRef Name="BusinessUnitId" /> 
    <PropertyRef Name="Month" /> 
    <PropertyRef Name="Year" /> 
    </Key> 
    <Property Name="AgencyId" Type="int" Nullable="false" /> 
    <Property Name="BusinessUnitId" Type="int" Nullable="false" /> 
    <Property Name="Month" Type="int" Nullable="false" /> 
    <Property Name="Year" Type="int" Nullable="false" /> 
    ... 
+0

您可以顯示堆棧嗎? – 2010-05-04 13:48:58

+0

你有一段代碼嗎?因爲大部分時間這會導致一個沒有實例化的對象。 – pipelinecache 2010-05-04 16:20:25

+0

如何在EDMX中映射密鑰?即,對於這個過程,SSDL中的''值是什麼? – 2010-05-04 21:04:38

回答

3

首先,框架試圖取消引用EntityKey的原因是您的查詢的MergeOption屬性設置爲AppendOnly。那裏沒有太大的驚喜,因爲AppendOnly是默認值。隨着AppendOnly,該框架將檢查加載到內存中每一個實體,並試圖修正內容的引用使得:

  1. 同一實體不能在兩個不同的地點被加載到內存的兩倍,即使你從數據庫中查詢它兩次。
  2. 任何引用該實體的實體將始終指向相同的引用。

實體通過EntityKey屬性進行標識,該屬性說明了您發佈的調用堆棧中對此屬性的引用。

因此,一個選項是將您正在運行的查詢中的MergeOption更改爲NoTracking。這通常是隻讀查詢的最佳選擇。請注意,在編輯其他實體時,返回的實體不能用作參考實體。

另一種選擇是找出爲什麼EntityKey爲空。我仍然沒有看到你的proc完整的EDMX,但是你的CSDL中的關鍵是非常複雜的,我認爲它沒有正確映射到實體,所以EF無法弄清楚如何構建正確的EntityKey它正在實現您proc的實體。

+1

我也得到這個例外。我的問題是我改變了數據庫中的一列以允許空值。我在我的數據模型中有一個View,它返回了這一列。視圖中的列將「實體關鍵字」設置爲true。由於此列現在允許爲空值,因此我必須將「實體密鑰」更改爲false,因爲它不能再用於爲此實體生成唯一密鑰,因爲它允許使用空值。 – 2012-05-24 14:33:46