Linq to entities. Add child to an existing parent RRS feed

  • Pregunta

  • Hi,

    I have a problem when I want to add a child to an existing parent. I have Applications and Modules with a relation 1-*. When I start the application I get a list of Applications with Modules, then I add a Module to the first Application and I call Save function in Data Layer but when I attach the Application the Module has the state unchanged. How I can do to change the state to added?
    I prove more possible solutions that I find in foros but any works fine.

    One of the solutions that I'm testing is:

      <Runtime.CompilerServices.Extension()> _
        Public Sub AttachUpdated(ByVal context As ObjectContext, ByVal objectDetached As EntityObject)
                If (objectDetached.EntityState = EntityState.Detached) Then
                    'Miraremos por cada propiedad y si es de tipo relacion iremos a updatarla.
                    For Each prop As Reflection.PropertyInfo In objectDetached.GetType.GetProperties
                        If prop.PropertyType.BaseType Is GetType(System.Data.Objects.DataClasses.RelatedEnd) Then
                            Dim relatedEnd As IRelatedEnd = CType(prop.GetValue(objectDetached, Nothing), IRelatedEnd)
                            For Each relatedItem In relatedEnd
                        End If
                End If
                Dim original As Object = Nothing
                If context.TryGetObjectByKey(objectDetached.EntityKey, original) Then
                    context.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached)
                End If
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

    I need help!

    martes, 16 de junio de 2009 9:31