我创建了一个空的asp.net项目,下载以下包:
Microsoft.OData.EntityFrameworkProvider -Pre
因此,我获得了WCF Data Services 5.6.0并支持Entity Framework 6.0。所有引用都成功添加到项目中。
然后我添加一个新的ado.net实体模型,并从数据库中只生成一个实体。然后,我使用以下代码添加WCF数据服务:
#if DEBUG
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
#endif
public class MyService : DataService<MyContext>
{
public static void InitializeService(DataServiceConfiguration config)
{
#if DEBUG
config.UseVerboseErrors = true;
#endif
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
但是当我在IE中查看服务时,我收到以下错误:
服务器在处理请求时遇到错误。异常消息是'表达式类型'System.Data.Entity.Core.Objects.ObjectContext'不能用于返回类型'System.Data.Objects.ObjectContext''。请参阅服务器日志以获取更多详异常堆栈跟踪是:
在System.Linq.Expressions.Expression.ValidateLambdaArgs(类型delegateType,Expression&amp; body,ReadOnlyCollection 1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable
1个参数)处于System.Data的System.Data.Services.Providers.DbContextHelper.CreateDbContextAccessor(Type type) System.Data.Services.DataService 1.CreateMetadataAndQueryProviders(IDataServiceMetadataProvider& metadataProviderInstance, IDataServiceQueryProvider& queryProviderInstance, Object& dataSourceInstance, Boolean& isInternallyCreatedProvider) at System.Data.Services.DataService
上的.Services.Providers.DbContextHelper.GetDbContextAccessor(Type type)System.Data.Services.DataService 1.HandleRequest() at System.Data.Services.DataService
的1.CreateProvider()1.ProcessRequestForMessage(Stream messageBody) System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin上的System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象[]输入,对象[]和输出)中的SyncInvokeProcessRequestForMessage(Object,Object [],Object []) MessageRpc&amp; rpc)System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(Mess ageRpc&安培; rpc)位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc),位于System的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)。 Service.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc),位于System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)
我错过了其他什么吗?
答案 0 :(得分:1)
问题解决了。必须在服务中添加以下内容:
using System.Data.Services.Providers;
并将基类更改为:
public class MyService : EntityFrameworkDataService<MyContext>