Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgraded to EF 6 (RTM) - Getting System.Data.Entity.Core.Objects.ObjectContext cannot be used for return type System.Data.Objects

Just upgraded a .NET 4.5 WCF Service, which also has an OData service to use EF 6. The OData service was of course working prior to the upgrade. Now, when attempting to query the OData service or even just browse to it from Visual Studio 2012, I get the following Request Error:

The server encountered an error processing the request. The exception message is 'Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext''. See server logs for more details.

The exception stack trace is:

at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection`1 parameters)
at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
at System.Data.Services.Providers.DbContextHelper.CreateDbContextAccessor(Type type)
at System.Data.Services.Providers.DbContextHelper.GetDbContextAccessor(Type type)
at System.Data.Services.DataService`1.CreateMetadataAndQueryProviders(IDataServiceMetadataProvider& metadataProviderInstance, IDataServiceQueryProvider& queryProviderInstance, Object& dataSourceInstance, Boolean& isInternallyCreatedProvider)
at System.Data.Services.DataService`1.CreateProvider()
at System.Data.Services.DataService`1.HandleRequest()
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[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

The ODataLib is 5.6, EF is 6.0.1, the WCF Data Services Server is 5.6.

This issue did not help. I have done everything from here as well with no luck. I have no references to System.Data.Entity. What can I do to get past this?

like image 740
Grandizer Avatar asked Oct 21 '13 16:10

Grandizer


People also ask

Which type of context is not supported in EF6?

Type of context 'System.Data.Entity.Core.Objects.ObjectContext' is not supported Note that the project have the reference updated to EF6 (system.data.entity.core) A new preview of Dynamic Data Provider and EntityDataSource control for EF6 has been released.

What is default mapping of entity in EF Core?

In EF Core 6.0, the entity is still mapped to a table using default mapping, even if it's also mapped to table-valued function. Table-valued functions which return entities are often used either as a helper or to encapsulate an operation returning a collection of entities, rather than as a strict replacement of the entire table.

What happened to its mapping in EF Core 6?

Its mapping has changed from RESTRICT to NO ACTION. This may cause a lot of operations to be generated in your first migration added after upgrading to EF Core 6.0. You can choose to either apply these operations or manually remove them from the migration since they have no functional impact on EF Core.

Can the join entity be used explicitly in EF Core?

Note that with this configuration, the join entity can be used explicitly, just like in previous versions of EF Core. However, the relationship can also be used as a many-to-many relationship.


1 Answers

Use the WCF Data Services Entity Framework Provider.

See Using WCF Data Services 5.6.0 with Entity Framework 6+ for more information.

Using WCF Data Services 5.6.0 with Entity Framework 6+

And now for some exciting news: you can finally use WCF Data Services with Entity Framework 6+! Today we are uploading a new NuGet package called WCF Data Services Entity Framework Provider. This NuGet package bridges the gap between WCF Data Services 5.6.0 and Entity Framework 6+. We were able to build this provider as an out-of-band provider (that is, a provider that ships apart from the core WCF DS stack) because of the public provider work we did recently.

Upgrading an existing OData service to EF 6

If you are upgrading an existing OData service to Entity Framework 6 or greater, you will need to do a couple of things:

  1. Install the WCF Data Services Entity Framework Provider NuGet package. Since this package has a dependency on WCF Data Services 5.6.0 and Entity Framework 6 or greater, some of the other NuGet packages in your project may be upgraded as well.
  2. Replace the base type of your DataService. For EF 5 or below, your data service should inherit from DataService<T> where T is a DbContext or ObjectContext. For EF 6 or greater, your data service should inherit from EntityFrameworkDataService<T> where T is a DbContext. See What’s the difference between DataService and EntityFrameworkDataService below for more details.

Creating a new OData service with EF 6

If you are creating a new OData service and would like to use Entity Framework 6 or greater, you will need to follow similar steps:

  1. Create your new project. I typically use an ASP.NET Empty Web Application for this, but you can use whatever you want. Note that if you do use the empty template, you may need to create an App_Data folder for Entity Framework to work properly with LocalDB.
  2. Install the WCF Data Services Entity Framework Provider NuGet package. Since this package has a dependency on WCF Data Services 5.6.0 and Entity Framework 6 or greater, some of the other NuGet packages in your project may be upgraded as well.
  3. Add a new WCF Data Service. It’s best if you ensure that your tooling is up-to-date as we occasionally fix bugs in the item template. Our latest tooling installer was released with WCF DS 5.6.0. It can be downloaded here.
  4. Replace the base type of the DataService that was generated by the item template. For EF 6 or greater, your data service should inherit from EntityFrameworkDataService<T> where T is a DbContext. See What’s the difference between DataService and EntityFrameworkDataService below for more details.

What’s the difference between DataService<T> and EntityFrameworkDataService<T>?

Historically the WCF DS stack required all WCF DS-based OData services to inherit from DataService<T>. Internally, the data service would determine whether the service should use the in-box EF provider, the in-box Reflection provider, or a custom provider. When we added support for EF 6, we utilized the new public provider functionality to allow the provider to ship separately. This will allow us, for instance, to use WCF DS 5.6.0 with either EF 5, 6, or some future version. However, the new public provider functionality comes with a little bit of code you need to write. Since that code should be the same for every default EF 6 WCF DS provider, we went ahead and included a class that does this for you. EntityFrameworkDataService<T> inherits from DataService<T> and implements all the code you would need to implement otherwise. By shipping this additional class, we literally made the upgrade process as simple as changing the base type of your service.

like image 200
tne Avatar answered Oct 18 '22 18:10

tne