How do I update properties of non-scalar objects in EF 4.0?

I first used this as an extension method to update my individual objects ...

Public Sub AttachUpdated(ByVal obj As ObjectContext, ByVal objectDetached As EntityObject)
    If objectDetached.EntityState = EntityState.Detached Then
        Dim original As Object = Nothing
        If obj.TryGetObjectByKey(objectDetached.EntityKey, original) Then
            obj.ApplyCurrentValues(objectDetached.EntityKey.EntitySetName, objectDetached)
        Else
            Throw New ObjectNotFoundException()
        End If
    End If
End Sub

      

Everything works fine until I had to update the non-scalar properties. Correct me if I am wrong, but that is because "ApplyCurrentValues" only supports scalars. To get around this, I just kept the FK_ID field instead of the object object relation. Now I have faced many, many relationships, so it is not that easy. I would like to do something like this ...

Dim Resource = RelatedResource.GetByID(item.Value)
Condition.RelatedResources.Add(Resource)

      

But when I call SaveChanges, the added Resources are not saved. I started playing with self-check objects (not sure if they will help solve my problem), but it seems that they cannot be serialized to ViewState and this is what I need.

I guess one solution would be to add the xRef table as an object and add the fks itself, but I would rather it just work as I expect.

I am open to any suggestions on how to either keep my many-to-many relationships, or serialize self-tracking objects (if self-monitoring can even solve my problem). Thanks!

+2


a source to share


1 answer


I think you can read this EF answer programticlly insert many-to-many first and then elaborate more on your problem.



+1


a source







All Articles