Update a single entity

Jul 16, 2012 at 8:39 PM
Edited Jul 17, 2012 at 7:01 PM

I am new to Entity Framework Model and nHydrate.

However, I have generated my EFDAL from my database design.  I can see you can save all changes by calling EFDAL.MyEntities.SaveChanges().

Is it possible to update/save a single entity without saving all of the data?  Is there a method I can call to do this?

Thanks for any help.

 

Andez 

 

Coordinator
Jul 17, 2012 at 5:29 PM

Yes there is a static method on the entity object where you can set 1 property on any number of rows with a Where statement. Below is an example of setting the Employee object's Dept field to a new value. Where it used to be "q", it will be reset to "w".

Acme.MyTest.EFDAL.Entity.Employee.UpdateData(x=>x.dept, x=>x.dept == "q", "w");

The caveat is that this only works for 1 field. You will need one line of code for each field set because it is a lamba statement with a single field.

Jul 17, 2012 at 7:29 PM

Hi,

Thanks for that.

Given that example, if there were a number of Employees with "q" as the department, would it set all of them to "w"?

As for my scenario, a user can create or edit a single entity in a single tab which contains it's property fields in the UI.  The user can click a save button to save the selected tab or save all to save all modified entities.

Whilst there is a static UpdateData, there is no equivalent static InsertData, so I cannot fulfill the above functionality by means of the Save click.

I was hoping that there would be a save method you could call on a entity instance, or a static save method where you could pass the entity reference.

Any other thoughts?

Thanks,

Andez

Coordinator
Jul 19, 2012 at 2:09 AM

It you wish to more complex data operations, then you will need to use Entity Framework as designed. You must create a new objects of some type, set its properties, add it to the context, and save the context. For updating you will need to load the object and set its properties and save the context. If you already have an entity reference that is hooked to a context, then all you must do is save the context. I hope this addresses your issue.

Jul 19, 2012 at 8:25 AM

Hi,

I have put the following in place.  It might not be the "best practice" but it seems to work.  It uses a second instance of entities context to do the update on and detaches the entity first before attaching to the second context instance.  When complete it reattaches to the original context.

Public Overrides Sub Save()
        ' save the project
        If _entity IsNot Nothing Then
            Dim entites As New MyEntities(My.Settings.ConnectionString)
            ' ToDo:  Store intial state
            _context.Detach(_entity)
            entites.Attach(_entity)

            ' forcefully change state - ToDo:  Set correct state
            entites.ObjectStateManager.ChangeObjectState(_entity, EntityState.Modified)

            entites.SaveChanges()
            entites.Detach(_entity)
            _context.Attach(_entity)

        End If
    End Sub
Thanks,
Andez