Detached Entity

Aug 9, 2011 at 1:24 PM
Before shifting to production with EFDAL, can you tell me the best method to Update or Insert with Detached entities
Fetch Titles
    Public Shared Function GetTitle() As List(Of Title)
        Using context = MyProjectEntities
            context.ContextOptions.LazyLoadingEnabled = False
            Return context.Title.Include(Function(x) x.Gender).Execute(Objects.MergeOption.PreserveChanges).ToList
        End Using
    End Function
there is a foreign key involved FK_Title_Gender On Title.GenderID(fk) --> Gender.ID(pk)

Put this list in DataGrid in UI
Add, Modify or Delete entity in UI
Send this list with detached entities back to DAL immediately after any delete, add or modify entity
Save Titles
    Public Shared Function SaveTitle(ByVal lst As List(Of Title)) As Integer
        Dim ret as Integer
        Dim context = MyProjectEntities
        For Each ps As Title In lst
            If ps.EntityState = EntityState.Modified Then
            ElseIf ps.EntityState = EntityState.Deleted Then
            ElseIf ps.EntityState = EntityState.Added Then
            End If
            ret = context.SaveChanges()
        Catch ex As Exception
        End Try
        Return ret
    End Function
Is this Correct ?
Aug 13, 2011 at 4:11 PM

I have never used detached entities so I am have no real advise. If an entity is detached you can simply add it to a domain so I am unsure how to answer the question. If an entity is modified it means that it is already part of a context so there is no need to do anything. I do not think you should be adding them to the same context or a new context. Why do you have detached entities. You can load a set of data and modify it in a grid. If you add a new entity, simply create a new entity and add it to the context. Changes are not saved until you call the SaveChanges method. The same goes for deletion. If you manipulate a context in real-time, it will keep up with changes with no need for detached entities. If you are using a web model (detached and stateless by nature), then you simply reload the entity and modify it in the same event.