2013-06-27 53 views
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 ; 
     } 
} 

書籍實體有一個屬於AuthorID外鍵。但是,客戶端需要做使用作者的姓名(未編號)補丁,併發送JSON作爲

{AuthorName : "Joe Smith"} 

AUTHORNAME是不是在書的模型。

我想要做的就是使用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> 
+0

你能分享你的$元數據? OData的定義補丁只結構特性,看起來像你正試圖修補導航屬性。如果您共享$元數據,我可以提出一種不同的方法。 –

+0

謝謝Raghu,非常簡單的模式 – LastTribunal

回答

1

從$元數據,它好像有書籍之間沒有任何關係和作者(我沒有在模型中看到任何導航屬性)。因此,使用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來~/Author(42)/UpdateAuthor

{ 
    'name' : 'Joe Smith' 
} 
+0

非常感謝Raghu,非常有幫助! – LastTribunal