views:

36

answers:

2

I always thought I understood how this works...but lately I have started really using interfaces and now things arent doing what I am expecting.

using entity framework, I have a service for each object that is responsible for interacting with the database and such....on my one service I am passing the collection of objects to my service as an icollection(of contactinfo) before I pass the object, the changetraker has the right states. however in my method, this is not the case and all states are set to unmodified.

Private Sub SaveExecute(ByVal param As Object)
            Dim srv As Services.ContactInfoService = GetService(Of Services.IContactInfoService)()

            If srv.SaveChanges(Me.ContactInfoCollection) Then
                GetEvent(Of Events.EditCompletedEvent(Of ICollection(Of Model.ContactInfo))).Publish(Me.ContactInfoCollection)
            End If


        End Sub



Public Function SaveChanges(ByVal con As ICollection(Of ContactInfo)) As Boolean Implements IContactInfoService.SaveChanges

            Using _context As New referee2Entities

                For i As Integer = 0 To con.Count - 1
                    _context.ContactInfoes.Attach(con(i))
                Next
                _context.DetectChanges()
                If _context.SaveChanges() > 0 Then
                    Return True
                    EnableNavigation()
                End If
                Return False
            End Using
            '  Return Save()

        End Function

As I said above the Me.contacInfoCollection has the right changetracking states. But Once its passed to the srv.savechanges it reverts to unmodified. I am sure its something silly I am missing...the whole EF thing can be confusing to me...

A: 

You need to attach before you modify.

Craig Stuntz
A: 

Actually that is not possible given the way I am using services for my data operations.

this is the way I have gotten it to work:

Public Function Update(ByVal con As ObservableCollection(Of ContactInfo)) As Boolean Implements IContactInfoService.Update


            Using _context As New referee2Entities
                Dim entry As ObjectStateEntry
                For Each c As ContactInfo In con
                    If c.ID = 0 Then
                        _context.ContactInfoes.AddObject(c)
                    Else
                        _context.ContactInfoes.Attach(c)
                        entry = _context.ObjectStateManager.GetObjectStateEntry(c)
                        entry.ChangeState(EntityState.Modified)
                        entry.ApplyCurrentValues(c)
                    End If
                Next
                Return Save(_context)

            End Using


        End Function
ecathell