Oops! Our OData Was Read Only. Yikes!

Jon Galloway and I recently documented what we were learning about Entity Framework Code-First and oData as part of the Full Stack project.  It turns out, however, that we were into some bleeding edge technology, and while   everything we posted works, it works read-only, as written it is not possible to update the data.  Not great.

With the guidance of Chris “Woody” Woodruff we stopped using batch updating and we opened up our error messages to see more detail.  This led Jon to an article by Rowan Miller which targeted exactly the problem we were having.  Jon documents it all here.

Key was that we were using DbContext, which is a wonderfully lightweight convention-over-configuration based wrapper over ObjectContext, but one of its limitations is that it does not does not include IUpdatable support, as documented here.

The answer, as documented in detail by Jon and written up in even more detail by Rowan is to expose the ObjectContext which you can do directly from the DbContext. 

public class  PersonTestDataService : DataService<ObjectContext>
    public static void InitializeService(DataServiceConfiguration config)
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    protected override ObjectContext CreateDataSource()
        var ctx = new PersonContext();
        var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
        objectContext.ContextOptions.ProxyCreationEnabled = false;
        return objectContext;

With this done, you must remember to update the client proxy using DataSvcUtil.exe as explained in the earlier article. 

Be sure to read Jon’s article on all of this.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Patterns & Skills and tagged , . Bookmark the permalink.