Adding a relation object in a generic / abstract way

I am trying to write a WCF method that will receive a separate EntityObject client from the client and be able to determine which properties and which relationships have changed for that object, comparing it to what is already in the context. Of course, if this object is a new entity or one of its relationships has been added / removed / changed, it must also recognize this and act accordingly.

I can already find out if the entity relation is new, but can't seem to add it correctly. -With any approach I am trying to get another exception.

Here is the method I use to update a single object:

public static void AttachUpdated(this ObjectContext context, EntityObject objectDetached)
{
    if (objectDetached.EntityState == EntityState.Detached)
    {
        object currentEntityInDb = null;

        if (context.TryGetObjectByKey(objectDetached.EntityKey, out currentEntityInDb))
        {
            context.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached);
            //Apply property changes to all referenced entities in context 
            context.ApplyReferencePropertyChanges((IEntityWithRelationships)objectDetached,
                                                  (IEntityWithRelationships)currentEntityInDb); //Custom extensor method 
        }
        else
        {
            //The entity should be added
            //?????
        }
    }
}

      

And this is the method I use to update entity relationships:

public static void ApplyReferencePropertyChanges(this ObjectContext context,
                                                         IEntityWithRelationships newEntity,
                                                         IEntityWithRelationships oldEntity)
{
    foreach (var oldRelatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
    {
        var oldRef = oldRelatedEnd as EntityReference;

        if (oldRef != null)
        {
            // this related end is a reference not a collection 
            var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;
            if (newRef.EntityKey != null)
            {
                oldRef.EntityKey = newRef.EntityKey;
            }
            else
            {   
                //When oldRed is a 1:Many relationship
                //newRef is an EntityReference<TEntity> object
                EntityObject entity = newRef.GetType().GetProperty("Value").GetValue(newRef, null) as EntityObject;
                oldRef.EntityKey = entity.EntityKey;
            }
        }
        else
        {
            IRelatedEnd newRelatedEnd = newEntity.RelationshipManager.GetRelatedEnd(oldRelatedEnd.RelationshipName, oldRelatedEnd.TargetRoleName);
            foreach (IEntityWithRelationships e in newRelatedEnd)
            {
                if (!oldRelatedEnd.Contains((e as IEntityWithKey).EntityKey))
                {
                    //this is a new relation and it needs to be added.
                    //???????
                }
                else
                {
                    //Find out if relation was modified - and update it if needed
                    //????????
                }
            }
            IEnumerable entities = oldRelatedEnd as IEnumerable;

        }
    }
}

      

How should this be done?

Please, help: (

0


a source to share


3 answers


Where did your ObjectContext come from? (I assume this is your Entity Framework database link)

There can be two problems here:

First, I don't think the ObjectContext is serializable, so if you send it to the client and then send it back, you will get an error.



Second, if you are storing the ObjectConext on the server, the server objects are by default per call and not per session, so you will be trying to bind your Entity to the new ObjectContext.

In our projects, we map Entity Framework Objects to Data Transfer Objects to send across WCF. What you are trying to do might be easier (maybe?) In the next version of Entity Framework.

0


a source


I'm not sure what you want to achieve - if you want to persist server side changes you can use ADO.Net Data Services. Is this the case?



0


a source


See Perseus :

Perseus is a small project designed to explore ways to exchange Entity Framework graphics over a WCF Services website. The main part of the project is the EntityBag, which stores a graph of the objects along with change tracking Information. It is hoped that no one will use this to store and transport something as nasty as the head of Medusa .; -)

0


a source







All Articles