0

私は以下のコードを持っています:

public class MyPatchController : EntitySetController<Books , int>
{ 
   protected override Books PatchEntity(int key, Delta<Books> patch)
        {
            var Book = db.books.FirstOrDefault(p => p.ID== key);
            if (Book == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            patch.Patch(Book);
            db.SaveChanges();
            return Book ;
        }
}

Books エンティティには AuthorID 外部キーがあります。ただし、クライアントは作成者の名前 (ID ではない) を使用して PATCH を実行する必要があり、json を次のように送信します。

{AuthorName : "Joe Smith"}

AuthorName は Book のモデルではありません。

私がやろうとしているのは、linq を使用して authorID を検索することですが、Odata では、パッチを適用するときにモデルを組み合わせて一致させることはできません。

これを機能させる方法はありますか?

注、モデルでナビゲーションを使用しようとしましたが、役に立ちませんでした

編集: $メタデータ:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
    <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Store.Models">
      <EntityType Name="BOOK">
        <Key>
          <PropertyRef Name="BOOK_ID"/>
        </Key>
        <Property Name="BOOK_ID" Type="Edm.Int32" Nullable="false"/>
        <Property Name="BOOK_NAME" Type="Edm.String"/>
        <Property Name="AUTHOR_ID" Type="Edm.Int32"/>
      </EntityType>
      <EntityType Name="AUTHOR">
        <Key>
          <PropertyRef Name="AUTHOR_ID"/>
        </Key>
        <Property Name="AUTHOR_ID" Type="Edm.Int32" Nullable="false"/>
        <Property Name="AUTHOR_NAME" Type="Edm.String"/>
      </EntityType>
    </Schema>
    <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Default">
      <EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
        <EntitySet Name="Author" EntityType="Store.Models.Author"/>
        <EntitySet Name="Book" EntityType="Store.Models.Book"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>
4

1 に答える 1

1

$metadata から、Books と Authors の間に関係がないように見えます (モデルにナビゲーション プロパティが表示されません)。したがって、OData でこれを行う方法は、Book でアクション「UpdateAuthor」を定義してから呼び出すことです。

モデルビルダーコード

var books = builder.EntitySet<Book>("books");
var updateAuthor = books.EntityType.Action("UpdateAuthor");
updateAuthor.Parameter<string>("name");

あなたのコントローラーコード

[HttpPost]
public void UpdateAuthor([FromODataUri]int key, ODataActionParameters parameters)
{
    string name = (string)parameters["name"];

    // patch the author of book with id 'key'.
}

この json を に POST し~/Author(42)/UpdateAuthorます。

{
    'name' : 'Joe Smith'
}
于 2013-06-27T21:59:26.753 に答える