none
Problem with update using EF 4.1 RRS feed

  • Question

  • I have this part of code. the problem is that when i try to update, it doesn't work (insert is working).

    I used codes like this, to update simpler entities and work fine.

    Someone can help me?

    using (Base.Context context = new Base.Context())
                    {
                        if (solicitation.id == 0)
                        {
                            context.Entry(solicitation.origin.city).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.status).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.unitDestiny).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.unitDestiny.city).State = System.Data.EntityState.Unchanged;

                            context.origin.Add(solicitation.origin);
                            context.requestor.Add(solicitation.requestor);
                            context.solicitation.Add(solicitation);
                        }
                        else
                        {
                            context.Entry(solicitation.origin).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.requestor).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.origin.city).State = System.Data.EntityState.Unchanged;
                            context.Entry(solicitation.status).State = System.Data.EntityState.Unchanged;

                            context.Entry(solicitation).State = EntityState.Modified;
                        }
                        context.SaveChanges();  
                    }

    Tuesday, July 30, 2013 5:56 PM

Answers

  • Hi Damon,

    I tried what you suggest, modifying the object, but did not work.

    The solution i found was to use store procedures for the update, since now i am updating only a few fields.

    Wednesday, August 7, 2013 2:50 PM

All replies

  • Hi Alexsander,

    Could you please share your more code or screenshots of errors here . If you get some exception information at the runtime, It would be better post it here.

    Have a nice day!

    Buenruo


    ALL FOR ONE AND ONE FOR ALL.

    Friday, August 2, 2013 7:15 AM
  • Hi Buenruo,

    The problem is, there is no exception, the code executes, but the changes are not persisted.

    Friday, August 2, 2013 11:56 AM
  • If a change this line:

    context.Entry(solicitation.status).State = System.Data.EntityState.Unchanged;

    to

    context.Entry(solicitation.status).State = System.Data.EntityState.Modified;

    this is persisted, but the other changes no.

    Friday, August 2, 2013 12:18 PM
  • Hi Alexsander,

    According to your description, we can understand the code in if area is the insert and in else area is the update and you create a new context. As you said that "insert is working", so let us compare insert and update. In the insert area you add the entity to the context(e.g context.origin.Add(solicitation.origin)), so it will be insert to your database. In update area, you only modify the entity's state that isn't in context, as far as I know that is unusefull, so if you want update the data, you should update the entity that in context instead of modify the entity that isn't in current context.

    Please modify your code as follows(e.g update the origin's city:

    var currentOrigin=context.solicitation.origin;
    currentOrigin.city=solication.origin.city;

    or

    DbSet<Solicitation> s=context.solicatation;
    s.Attach(solication);

    Thanks

    Best Regards

    Saturday, August 3, 2013 3:53 AM
  • Hi Starain09

    Thanks, but tha didn't solve the problem

    Monday, August 5, 2013 4:56 PM
  • Hi Alexsander,

    Welcome to MSDN Forum.  Thank you for posting in MSDN Forum.

    According to your description of the problem, the issue you are experiencing is you have changed nothing but model’s state during the update scope.

    Based on my analysis of the issue, you have generated an instance named “solicitation”, check out the property named “id” of the instance. If value equals 0 do insert, else do update.

    If I have misunderstood anything, please feel free to let me know.

    So in our case, you should do some update to your model. Such as the below coding.

    else
    {
        context.Entry(solicitation.origin).State = System.Data.EntityState.Unchanged;
        context.Entry(solicitation.requestor).State = System.Data.EntityState.Unchanged;
        context.Entry(solicitation.origin.city).State = System.Data.EntityState.Unchanged;
        context.Entry(solicitation.status).State = System.Data.EntityState.Unchanged;
                         
        context.Entry(solicitation).State = EntityState.Modified;
        //add some update code here
        solicitation.origin.city=”New York”;
        ………
     }
    I hope this will help resolve your problem. If anything is unclear, please free feel to let us know.

    Best Regards,

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 7, 2013 5:20 AM
  • Hi Damon,

    I tried what you suggest, modifying the object, but did not work.

    The solution i found was to use store procedures for the update, since now i am updating only a few fields.

    Wednesday, August 7, 2013 2:50 PM