2017-04-09 18 views
0

私が持っていることはodata v4クライアントコードジェネレータ:どのように深い挿入を行うには?

var item1 = new Item { CODE = "ABC1", NAME = "A B C 1" }; 
var item2 = new Item { CODE = "ABC2", NAME = "A B C 2" }; 
var items = new DataServiceCollection<Item >{ item1, item2 }; 
var mt = new MyType { CURRDATE = DateTime.Now.toString(), ITEMS = items }; 
_container.AddToMyType(mt); 
var resp = _container.SaveChanges(); 
//...etc 

のようなものが正しいということはありますか?それは私の知る限りでは

"Unhandled Exception: System.InvalidOperationException: An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection."

答えて

0

について何かを訴えています、のOData v4のクライアントが深い挿入をサポートしていません。

これを実行する1つの方法は、ODataアクションメソッドを作成し、そのエンティティをシリアル化してから、ODataアクションメソッドに送信し、文字列として送信し、サーバー側で逆シリアル化します。私は、下記ている

別のオプションを使用すると、親/子で「POST」を行うようになる。

/// <summary> 
/// Deep insert parent and child. 
/// </summary> 
/// <param name="parentEntityPluralName"></param> 
/// <param name="entity"></param> 
public TEntity SaveWithChildren<TEntity>(string parentEntityPluralName, TEntity entity) where TEntity : BaseEntityType 
{ 
    // need to serialize the entity so that we can send parent and child together 
    string serializedEntity = Newtonsoft.Json.JsonConvert.SerializeObject(entity, new Newtonsoft.Json.JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); 

    // create a handler for the httpclient 
    using (System.Net.Http.HttpClientHandler httpHandler = new System.Net.Http.HttpClientHandler()) 
    { 
     // create the httpclient and add the handler 
     using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(httpHandler)) 
     { 
      // setup the headers 
      httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Prefer", @"odata.include-annotations=""*"""); 
      httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json;odata.metadata=minimal"); 

      // setup the content to send 
      using (System.Net.Http.StringContent odataContent = new System.Net.Http.StringContent(serializedEntity)) 
      { 
       // setup the content type to json 
       odataContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 

       // post the data to the odata service 
       using (System.Net.Http.HttpResponseMessage response = httpClient.PostAsync(this.BaseUri + parentEntityPluralName, odataContent).Result) 
       { 
        // get back any errors or content 
        string content = response.Content.ReadAsStringAsync().Result; 

        // show error if service failed 
        if (response.IsSuccessStatusCode == false) 
        { 
         throw new Exception(content); 
        } 

        // try to convert the object back from the service call 
        return Newtonsoft.Json.JsonConvert.DeserializeObject<TEntity>(content); 
       } 
      } 
     } 
    } 
} 

場所あなたの「代理」生成されたクラスに部分的で新しいクラスで、このメソッド。
次に、Containerクラスを呼び出して、SaveWithChildrenメソッドを呼び出します。
これは親コントローラPOSTメソッドと呼ばれます。
親コントローラPOSTメソッドを呼び出すだけで、サーバー上の子を反復処理する必要があります。

+0

ご迷惑をおかけしますが、私は深い挿入が不可能であり、あなたのようないくつかの答えを期待していたが、RestSharpを使用して終了したが、あなたの答えに感謝した。私は将来チャンスを取るときにそれを試してみます。感謝! ありがとう! – lock22

関連する問題