简体   繁体   English

可以使用相关实体(而不是相关ID)使用OData创建实体吗?

[英]Can one create an entity with OData using related entities (not the related IDs)?

I'm not exactly sure if this is an OData issue, WCF , EF , or what; 我不确定这是OData问题, WCFEF还是什么? I'm new to this and those lines are still blurry. 我是新来的,这些线条仍然模糊。

I've been trying to create some entities using an OData service in a C# MVC .NET app. 我一直在尝试使用C#MVC .NET应用程序中的OData服务创建一些实体。 Doing it by using the IDs for related entities seems to work fine, eg: 通过使用相关实体的ID来执行此操作似乎很好,例如:

Container c = getContainer();
Foo f = new Foo();
f.Name = "blah";
f.ThingId = 7;
c.AddToFoos(f);
c.SaveChanges();

However, using related entities directly doesn't work. 但是,直接使用相关实体无效。 This did work for me when using a local SQL database, but not with the OData service. 当使用本地SQL数据库而不是OData服务时,这确实对我有用。 Eg: 例如:

Container c = getContainer();
Foo f = new Foo();
Thing t = c.Things.Where(v => v.id==7).FirstOrDefault();
f.Name = "blah";
f.Thing = t;
c.AddToFoos(f); // or Attach(f)
c.SaveChanges();

Additionally, using SetLink doesn't work either. 此外,使用SetLink也不起作用。 In all the failure cases, the Id is set to 0 in the request. 在所有失败情况下,请求中的Id都设置为0。 In the SetLink case it had an @odata.bind parameter. SetLink情况下,它具有一个@odata.bind参数。

Since the IDs are 0 in the request, this seems to be a client-side mapping issue. 由于请求中的ID为0,因此这似乎是客户端映射问题。 Is it possible to get this behavior to work? 是否有可能使这种行为起作用?

This is related to a feature called Batch. 这与称为批处理的功能有关。 You can just try code like this: 您可以尝试这样的代码:

Container c = getContainer();
Foo f = new Foo();
f.Name = "blah";
c.AddToFoos(f); 

Thing t = c.Things.Where(v => v.id==7).FirstOrDefault();
c.AddLink(f, "Thing", t); // Thing is the Navigation property name from f to t.

c.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM