2009-05-13 16 views
0

クライアントからデタッチされたEntityObjectを受け取るWCFメソッドを作成しようとしています。すでに文脈にある。 もちろん、このエンティティが新しいエンティティである場合、またはその関係の1つが追加/削除/変更された場合、そのエンティティもそれを認識してそれに応じて動作する必要があります。汎用エンティティ/抽象メソッドで関係エンティティを追加します。

エンティティの関係が新しいものかどうかはすでに認識できますが、正しく追加できないようです。私が試みるあらゆるアプローチでは、私は別の例外を得る。ここで

は、私は分離オブジェクトを更新するために使用する方法である:

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 
      //????? 
     } 
    } 
} 

そして、これは私は、エンティティの関係を更新するために使用する方法である:

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; 

     } 
    } 
} 

は、どのようにそれを実装する必要がありますか?

助けてください:(

答えて

0

どこのObjectContextから来ている(これはあなたのEntity Frameworkのデータベースの参照であることを私は仮定しています)

ここでは二つの問題があるかもしれません:?まず

、I

第2に、ObjectConextをサーバー上に保持している場合、サーバーオブジェクトはdefで指定されています。つまり、ObjectContextはシリアライズ可能であるとは思わないので、クライアントに送信するとエラーが返されます。セッションごとではなく、呼び出し毎に、したがって、あなたは新しいObjectContextにあなたのEntityを関連づけようとしています。

プロジェクトでは、エンティティフレームワークオブジェクトをデータ転送オブジェクトにマップして、WCFを送信します。エンティティフレームワークの次のバージョンでは、何をしようとしているのがより簡単になる可能性がありますか?

0

私はあなたが達成したいとは思っていません - サーバー側の変更を保存する場合は、ADO .Net Data Servicesを使用できます。それは事実ですか?

0

Perseusを参照してください:

ペルセウスは、WCFのWebサービス 上 Entity Frameworkのエンティティのグラフを交換するための方法を模索 に設計された小さなプロジェクトです。プロジェクト の重要な部分は、エンティティのグラフ と、変更トラッキング の情報を格納するEntityBagです。ここに誰も望んでいないでしょう &輸送 メデューサの頭のようなものとして何かを格納するためにこれを使用します。 ;-)

関連する問題