简体   繁体   中英

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; 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. Doing it by using the IDs for related entities seems to work fine, eg:

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. 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. In all the failure cases, the Id is set to 0 in the request. In the SetLink case it had an @odata.bind parameter.

Since the IDs are 0 in the request, this seems to be a client-side mapping issue. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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