Entity framework 使用查找修补实体
我的代码如下:Entity framework 使用查找修补实体,entity-framework,odata,asp.net-web-api,Entity Framework,Odata,Asp.net Web Api,我的代码如下: 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
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 ;
}
}
AuthorName不在书本模型中
我正在尝试使用linq查找authorID,但Odata不允许我在修补时混合和匹配模型
有没有办法让这一切顺利进行
注意,我已经尝试在模型中使用导航,但没有效果
编辑:
$metadata:
<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>
从$metadata来看,书籍和作者之间似乎没有任何关系(我在模型中没有看到任何导航属性)。所以,使用OData实现这一点的方法是在Book上定义一个动作“UpdateAuthor”,然后调用它 模型生成器代码
var books = builder.EntitySet<Book>("books");
var updateAuthor = books.EntityType.Action("UpdateAuthor");
updateAuthor.Parameter<string>("name");
您可以将此json发布到~/Author(42)/UpdateAuthor
{
'name' : 'Joe Smith'
}
你能分享你的美元吗?OData仅为结构属性定义修补程序,看起来您正在尝试修补导航属性。不过,如果您共享$metadata,我可以建议一种不同的方法。谢谢Raghu,非常简单的schemathank很多Raghu,非常有用!
[HttpPost]
public void UpdateAuthor([FromODataUri]int key, ODataActionParameters parameters)
{
string name = (string)parameters["name"];
// patch the author of book with id 'key'.
}
{
'name' : 'Joe Smith'
}