Stored procedures 带有'的实体框架;获取';返回实体的存储过程
我试图执行一个存储过程,该过程返回的数据列与我在项目中拥有的表实体的列完全相同。我将“添加函数导入”对话框中的“返回集合”属性设置为我的实体类型 我在执行存储过程时遇到了一个NullReferenceException错误,在进一步挖掘时,似乎是因为缺少“EntityKey”属性。我能告诉它忽略实体的那些特殊属性吗 我已经为该实体创建了一个带有“[ScaffoldColumn(false)]”的分部类,但这似乎并不重要 干杯, 尼克 编辑: 以下是堆栈跟踪: [NullReferenceException:对象引用未设置为对象的实例。] System.Data.EntityKey.AddHashValue(Int32 hashCode,Object keyValue)+36 System.Data.EntityKey.GetHashCode()+7696654 System.Collections.Generic.GenericEqualityComparerStored procedures 带有'的实体框架;获取';返回实体的存储过程,stored-procedures,entity-framework-4,Stored Procedures,Entity Framework 4,我试图执行一个存储过程,该过程返回的数据列与我在项目中拥有的表实体的列完全相同。我将“添加函数导入”对话框中的“返回集合”属性设置为我的实体类型 我在执行存储过程时遇到了一个NullReferenceException错误,在进一步挖掘时,似乎是因为缺少“EntityKey”属性。我能告诉它忽略实体的那些特殊属性吗 我已经为该实体创建了一个带有“[ScaffoldColumn(false)]”的分部类,但这似乎并不重要 干杯, 尼克 编辑: 以下是堆栈跟踪: [NullReferenceExce
1.GetHashCode(T obj)+23
系统.集合.通用.词典
2.FindEntry(TKey)+70
System.Collections.Generic.Dictionary2.TryGetValue(TKey、TValue和value)+17
System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey、EntityEntry和entry)+111
System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func
2 ConstructionEntityDelegate、EntityKey、EntityKey、EntitySet-EntitySet)+102
lambda_法(闭合、整形)+460
System.Data.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper-Shaper)+170
System.Data.Common.Internal.Materialization.SimpleNumerator.MoveNext()+84
System.Collections.Generic.List
1..ctor(IEnumerable1 collection)+406
System.Linq.Enumerable.ToList(IEnumerable
1源代码)+58
C:\dasdsd\Controllers\ReportsController.cs:72中的TWAgencySpend.Controllers.ReportsController.Advertising(可空1 agencyId,可空1 businessUnitId)
lambda_方法(闭包、控制器基、对象[])+190
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)+51
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary2参数)+409
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary
2个参数)+52
System.Web.Mvc.c__显示类d.b__a()+127
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func1 continuation)+436
System.Web.Mvc.c___显示类f.b___c()+61
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext ControllerContext,IList
1过滤器,ActionDescriptor ActionDescriptor,IDictionary2参数)+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__显示类8.b__4()+65
System.Web.Mvc.Async.c__显示类1.b__0()+44
System.Web.Mvc.Async.c_uuudisplayClass8
1.b_uuu7(IAsyncResult)+42
System.Web.Mvc.Async.WrappedAsyncResult`1.End()+141
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+54
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+52
System.Web.Mvc.MvcHandler.System.Web.IHTTPassynchandler.EndProcessRequest(IAsyncResult结果)+38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+8836913
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+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>
以下是我的实体的关键:
<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" />
...
...
首先,框架尝试取消引用EntityKey
的原因是,查询的MergeOption
属性设置为AppendOnly
。这并不奇怪,因为默认设置是AppendOnly
。使用AppendOnly
,框架将检查加载到内存中的每个实体,并尝试修复引用,以便:
同一实体不能在两个不同的位置两次加载到内存中,即使您从数据库中查询了两次
引用该实体的任何实体将始终指向同一引用
实体是通过EntityKey
属性标识的,该属性说明了对您发布的调用堆栈中该属性的引用
因此,一个选项是将正在运行的查询上的MergeOption
更改为NoTracking
。这通常是只读查询的最佳选择。请注意,在编辑其他实体时,返回的实体不能用作引用实体
另一个选项是找出EntityKey
为空的原因。我仍然看不到您的进程的完整EDMX,但CSDL中的键非常复杂,我认为它只是没有正确地映射到实体,因此EF无法在从您的进程具体化实体时确定如何构造正确的EntityKey
。首先,框架尝试取消引用EntityKey
的原因是查询的MergeOption
属性设置为AppendOnly
。这并不奇怪,因为默认设置是AppendOnly
。用追加
<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" />
...