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.

Share

About Jesse Liberty

Jesse Liberty is an independent consultant and programmer with three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been 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, Microsoft MVP and Telerik MVP.
This entry was posted in Patterns & Skills and tagged , . Bookmark the permalink.

One Response to Oops! Our OData Was Read Only. Yikes!

  1. Pingback: Tweets that mention Oops! Our OData Was Read Only. Yikes! | Jesse Liberty -- Topsy.com

Leave a Reply

Your email address will not be published.