locked
can't save the data UpdateObject error RRS feed

  • Question

  • Hi,

    I have WCF data service based on a Entity Model defined, in the model I have a View and associated stored procedure for the updates.

    In the code I define the:

           

    DataServiceCollection<MyEntity> MyEntity;
    
      MyModelEntities MyDataContext;
    
    


            
            call async the service, I receive all my data fine, and bind my grid source to the entity
            
           

    MyGrid.ItemsSource = MyEntity; 
    
    

    Everything works fine, then I have an button click event and saving data does not work:
            
           

    MyDataContext.UpdateObject(MyEntity);
    
      MyDataContext.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, MyDataContext);
    
    

            
            I have the error: {System.ArgumentException: The context is not currently tracking the entity.
           
            when try to execute UpdateObject line, and that is weird give the fact that the entity was created by using the data context like this:

          

    MyDataContext = new ServiceReference.MyEntities(new Uri("http://localhost:8088/MyService.svc"));
    
     
    
      MyEntity = new DataServiceCollection<MyEntity>(MyDataContext);
    
    

            
            

    any help is appreciated.

     

    thanks
                        
    Note: I have a view as MyEntity and in the model I also have a stored procedure for the update and all needed EntityTypeMapping, actually the stored procedures is executed fine, but because the entity is not updated actually cant save the data. The entity is not updated because the views a read only, but my uderstanding is that if you add an update function it will not be read only. when I inspect the MyEntity I see it still has a IsTraking = true, but MyDataContext is false:

    var ApplyingChanges = MyDataContext.ApplyingChanges;

     

     

     

    and I have:

    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

     

     

     

     

    So if you can point me to ANY working sample with WCF Data services using a view as an entity and a stored procedure for the updates my problem is solved. That is really basic finctionality cant make work here and unfortunately the MSDN is not what it used to be before the latest .NET 4.0

    Friday, July 23, 2010 4:45 PM

All replies

  • Ha it looks like I am not alone interested in views constructing (updateable) WCF data service 8;p

    Though my story is a bit different from that of Volko665, I think it may still of value: how to make an odata service which allows insertion through updateable view (some people use updatable view)

    We all know views in database; it is relatively str8forward (thanks to the wonderful entity framework) to make an WCF data service presenting a feed to return what in the viw. However, the feed is readonly, no matter what EntitySetRight is set to the feed. My goal of this story is, to make the INSERT operation (in OData jargon, POST method) go through the view and reach whatever tables that consists of the view.

    First, it should be clear that read-only view is no good here; what we need here is a so-called "updateable view". There are two ways to archive an updateable view: one is database somehow figures it out - only if all the tables involve in happen to be quite simple and trivial to combine. This would be nice, however, more likely the view is not that simple. another way is defining a trigger (instead of insert etc) on the view - this is the interesting means which gives us desired levy to craft the update logic of involved tables. 

    For brevity I skip demonstrate how to create an updateable view by using intead-of trigger (see http://www.ibm.com/developerworks/data/library/techarticle/0210rielau/0210rielau.html for concrete samples).

    Second, let Entiti Framework model wizard help generate edmx/csdl and EF class definitions. Remember choose view in the wizard. Notice this generated edmx only allows READ. So we shall "heck" it a bit: find the entityset of the view, manually change its attributes store:Type="Tables" Schema:"dbo" (remove the namespace of store), and eliminate whole <DefinningQuery> node - no need any more since it is hecked like a real table.

    AlexJ has a blog devoted to this heck - http://blogs.msdn.com/b/alexj/archive/2009/09/01/tip-34-how-to-work-with-updatable-views.aspx.

    After that, go ahead to setup odata service and make sure th efeed of view is granted read and append/insertion. Request of POST method (insertin of entry) should be handled properly by WCF data service and EF, and eventually transformed to SQL statement like "insert in view_foo values (....)". This insertion would succeed, since we already make the view updateable.

    Whenever anything in doubt, fiddler request builder is a valuable debugger tool.

    Thursday, September 15, 2011 4:27 AM
    Moderator