当属性设置器是内部时,ADO.NET数据服务实体框架请求错误

时间:2022-07-19 02:11:26

I receive an error message when exposing an ADO.NET Data Service using an Entity Framework data model that contains an entity (called "Case") with an internal setter on a property. If I modify the setter to be public (using the entity designer), the data services works fine.

我在使用实体框架数据模型公开ADO.NET数据服务时收到错误消息,该数据模型包含一个属性上有内部setter的实体(称为“Case”)。如果我将setter修改为公共(使用实体设计器),则数据服务可以正常工作。

I don’t need the entity "Case" exposed in the data service, so I tried to limit which entities are exposed using SetEntitySetAccessRule. This didn’t work, and service end point fails with the same error.

我不需要在数据服务中公开实体“Case”,因此我尝试使用SetEntitySetAccessRule限制公开哪些实体。这不起作用,服务端点失败并出现相同的错误。

public static void InitializeService(IDataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("User", EntitySetRights.AllRead);
}

The error message is reported in a browser when the .svc endpoint is called. It is very generic, and reads “Request Error. The server encountered an error processing the request. See server logs for more details.” Unfortunately, there are no entries in the System and Application event logs.

调用.svc端点时,将在浏览器中报告错误消息。它非常通用,并显示“请求错误。服务器遇到处理请求的错误。有关详细信息,请参阅服务器日志。“遗憾的是,系统和应用程序事件日志中没有条目。

I found this * question that shows how to configure tracing on the service. After doing so, the following NullReferenceExceptoin error was reported in the trace log.

我发现这个*问题显示了如何在服务上配置跟踪。执行此操作后,跟踪日志中报告了以下NullReferenceExceptoin错误。

Does anyone know how to avoid this exception when including an entity with an internal setter?

当包含具有内部setter的实体时,是否有人知道如何避免此异常?

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131076</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2008-11-05T22:30:44.1523578Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{da77ee97-960f-4275-a5e7-a181c0b024b1}" />
<Execution ProcessName="WebDev.WebServer" ProcessID="6388" ThreadID="8" />
<Channel />
<Computer>MOTOJIM</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier>
<Description>Handling an exception.</Description>
<AppDomain>685a2910-19-128703978432492675</AppDomain>
<Exception>
<ExceptionType>System.NullReferenceException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace>
at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMemberMetadata(ResourceType resourceType, MetadataWorkspace workspace, IDictionary`2 entitySets, IDictionary`2 knownTypes)
at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 entitySets)
at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata()
at System.Data.Services.DataService`1.CreateProvider(Type dataServiceType, Object dataSourceInstance, DataServiceConfiguration&amp; configuration)
at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest()
at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody)
at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<ExceptionString>System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMemberMetadata(ResourceType resourceType, MetadataWorkspace workspace, IDictionary`2 entitySets, IDictionary`2 knownTypes)
   at System.Data.Services.Providers.ObjectContextServiceProvider.PopulateMetadata(IDictionary`2 knownTypes, IDictionary`2 entitySets)
   at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata()
   at System.Data.Services.DataService`1.CreateProvider(Type dataServiceType, Object dataSourceInstance, DataServiceConfiguration&amp; configuration)
   at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest()
   at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody)
   at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>

4 个解决方案

#1


2  

I recently ran into a similar issue, and the fix in my case was changinghe the name of the EntitySetAccessRule to "*", like this:

我最近遇到了类似的问题,我的案例中的修复程序是将EntitySetAccessRule的名称更改为“*”,如下所示:

public static void InitializeService(DataServiceConfiguration config)
{
  config.SetEntitySetAccessRule("*", EntitySetRights.All);
  config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

#2


0  

I had a similar problem, and it was caused by passing the wrong class to DataService. You need to pass the data entities class of the data model.

我有一个类似的问题,它是由错误的类传递给DataService引起的。您需要传递数据模型的数据实体类。

public class WebDataService : DataService< DataModel.DataEntities >

#3


0  

I had the same problem, and the solution turned out to be very simple: although my table name and the generated entity object is the singular "Order", I need to refer to it in this context as "Orders". Still getting my head around that. just EDM standard.

我有同样的问题,解决方案结果非常简单:虽然我的表名和生成的实体对象是单数“Order”,但我需要在此上下文中将其称为“Orders”。还是要理解我的想法。只是EDM标准。

config.SetEntitySetAccessRule("Orders", EntitySetRights.All);

Hope this helps.

希望这可以帮助。

#4


0  

I've had similar issue caused by EF6.

我有类似的问题由EF6引起。

According to this article changing

根据这篇文章的变化

public class WebDataService : DataService< DataModel.DataEntities >

to

public class WebDataService : EntityFrameworkDataService< DataModel.DataEntities >

should help

#1


2  

I recently ran into a similar issue, and the fix in my case was changinghe the name of the EntitySetAccessRule to "*", like this:

我最近遇到了类似的问题,我的案例中的修复程序是将EntitySetAccessRule的名称更改为“*”,如下所示:

public static void InitializeService(DataServiceConfiguration config)
{
  config.SetEntitySetAccessRule("*", EntitySetRights.All);
  config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}

#2


0  

I had a similar problem, and it was caused by passing the wrong class to DataService. You need to pass the data entities class of the data model.

我有一个类似的问题,它是由错误的类传递给DataService引起的。您需要传递数据模型的数据实体类。

public class WebDataService : DataService< DataModel.DataEntities >

#3


0  

I had the same problem, and the solution turned out to be very simple: although my table name and the generated entity object is the singular "Order", I need to refer to it in this context as "Orders". Still getting my head around that. just EDM standard.

我有同样的问题,解决方案结果非常简单:虽然我的表名和生成的实体对象是单数“Order”,但我需要在此上下文中将其称为“Orders”。还是要理解我的想法。只是EDM标准。

config.SetEntitySetAccessRule("Orders", EntitySetRights.All);

Hope this helps.

希望这可以帮助。

#4


0  

I've had similar issue caused by EF6.

我有类似的问题由EF6引起。

According to this article changing

根据这篇文章的变化

public class WebDataService : DataService< DataModel.DataEntities >

to

public class WebDataService : EntityFrameworkDataService< DataModel.DataEntities >

should help