none
EF4 POCO and REST Web service RRS feed

  • Question

  • Hello,

    I have a weird problem with my entity framework project. I have a solution with 3 projects :

    MyProject.Model : Which contains my model (.edmx)
    MyProject.POCO : Which contains my POCO classes generated with the ADO.NET POCO generator T4 template.
    MyProject.Service : This is a REST web service which references the POCO project and expose some CRUD methods

    I succeed in in creating and deleting objects and persist them in the database, but I don't know how to update an entity from a POCO sent to my web service.

    So, I have an operation in my REST service (with PUT http method) which takes a "Vehicle" POCO. I would like to update this "Vehicle" in the database but I don't know how do to because my web operation opens a ObjectContext and try to process this "detached" POCO. I don't know if I must attached it before or not. The only thing my code did is to duplicate the entry in my table instead of update it :(((.

    I need help

    Wednesday, June 15, 2011 2:51 PM

Answers

  • Hi,

    To update the database with an existing POCO, you first need to attach the POCO entity to your context, and then you need to modify the objectstate of the POCO entity.

    Example:

    using(MyModel context = new MyModel())
    {
      context.Vehicles.Attach(vehicle); //Adds the entity to the context in a Added state 
      context.ObjectStateManager.ChangeObjectState(vehicle, EntityState.Modified); //Change the entity state to modified to trigger an update
    
      context.SaveChanges();
    }
    

    This should update your existing database object.

    Hope this helps!

     


    --Rune
    Wednesday, June 15, 2011 7:19 PM
  • Hi,

    In the example you are referring to, the new OrderLine POCO object is first added to the Order entity OrderLine collection, then the DetectChanges call synchronizes this into the ObjectStateManager (since the OrderLIne isn't there from earlier it will have a state of EntityState.Added). When SaveChanges is called later, the state of the OrderLine in ObjectStateManger will change to EntityState.Unchanged.

    But, when they are modifying the OrderLine POCO object, the ObjectStateManager won't be updated automatically, so to reflect this change you can call DetectChanges to synchronize the EntityState for the OrderLine object in the ObjectStateManger to EntityState.Modified. (There are other ways to do this too, but this is what the example does)

    This is not quite what you are doing/want to do, since you from the beginning doesn't have your POCO object in the ObjectStateManager from earlier.

    Hope this explains it! If not feel free to ask more :)


    --Rune
    Thursday, June 16, 2011 7:48 AM

All replies

  • Hi,

    To update the database with an existing POCO, you first need to attach the POCO entity to your context, and then you need to modify the objectstate of the POCO entity.

    Example:

    using(MyModel context = new MyModel())
    {
      context.Vehicles.Attach(vehicle); //Adds the entity to the context in a Added state 
      context.ObjectStateManager.ChangeObjectState(vehicle, EntityState.Modified); //Change the entity state to modified to trigger an update
    
      context.SaveChanges();
    }
    

    This should update your existing database object.

    Hope this helps!

     


    --Rune
    Wednesday, June 15, 2011 7:19 PM
  • Ok, thank you, it works!!!

    So, just one more question : I found this documentation in the MSDN : http://msdn.microsoft.com/en-us/library/dd456854.aspx

    In this example they are using 

    context.ObjectStateManager.TryGetObjectStateEntry(newItem, out entry);

    and 

    context.DetectChanges();

    So, i'am a little bit confused. When should we use the DetectChanges() method?

     

    Thank you

     

     

     

     

    Thursday, June 16, 2011 6:18 AM
  • Hi,

    In the example you are referring to, the new OrderLine POCO object is first added to the Order entity OrderLine collection, then the DetectChanges call synchronizes this into the ObjectStateManager (since the OrderLIne isn't there from earlier it will have a state of EntityState.Added). When SaveChanges is called later, the state of the OrderLine in ObjectStateManger will change to EntityState.Unchanged.

    But, when they are modifying the OrderLine POCO object, the ObjectStateManager won't be updated automatically, so to reflect this change you can call DetectChanges to synchronize the EntityState for the OrderLine object in the ObjectStateManger to EntityState.Modified. (There are other ways to do this too, but this is what the example does)

    This is not quite what you are doing/want to do, since you from the beginning doesn't have your POCO object in the ObjectStateManager from earlier.

    Hope this explains it! If not feel free to ask more :)


    --Rune
    Thursday, June 16, 2011 7:48 AM
  • Thursday, June 16, 2011 8:27 AM