Answered by:
Inserting records via OData

Question
-
Are there any examples on how to directly add data to a Lightswitch application via OData? I've been struggling with a validation error that seems to indicate that I'm missing some required field. How am I supposed to handle the Id field or the RowVersion stuff?
I'm out of ideas about what's causing this. I've manually pieced together every relationship that I need. The only thing I think I'm missing from the OData documentation is calling AddLink(), but I get an error that the entities aren't being tracked.
What else is required?
EDIT: I really just need to do a bulk conversion of the data. I'd accept a solution that allows me to directly put data into the SQL tables, but how do I generate the RowVersion value?- Edited by kettch Thursday, June 13, 2013 6:28 PM
Thursday, June 13, 2013 6:24 PM
Answers
-
No need to worry about IDs and RowVersions, especially not for an INSERT !
I tried the following from a .net console app, which has a service reference to the odata service of a LS app.
Following "bric/brac" code is doing exactly what you want:
class Program { static void Main(string[] args) { ServiceReference2.ApplicationData ctx = new ServiceReference2.ApplicationData(new Uri(@"http://localhost:23286/ApplicationData.svc")); var customers = ctx.Customers; //ctx.Credentials = new NetworkCredential("test", "test"); foreach (var item in customers) { Console.WriteLine(item.LastName); } //adding a new customer Customer c = new Customer { LastName = "customer by command line process", FirstName="FindMeLater" }; ctx.AddToCustomers(c); ctx.SaveChanges(); Customer existing = ctx.Customers.Where(cc => cc.FirstName == "FindMeLater").First(); existing.LastName = "now we update again"; ctx.UpdateObject(existing); ctx.SaveChanges(); Console.Read(); } }
paul van bladel
- Proposed as answer by Huy Nguyen MSFTMicrosoft employee Wednesday, June 26, 2013 9:58 PM
- Marked as answer by Angie Xu Monday, July 8, 2013 2:17 AM
Thursday, June 13, 2013 7:08 PM
All replies
-
I don't think you'd want to generate a new RowVersion, but pass in the existing one. If you use F12 in your favorite browser to monitor network traffic, you'll see the current RowVersion value of a record is used in the etag when posting an update to a record.
- Edited by Alan M. _ Thursday, June 13, 2013 6:37 PM
Thursday, June 13, 2013 6:36 PM -
No need to worry about IDs and RowVersions, especially not for an INSERT !
I tried the following from a .net console app, which has a service reference to the odata service of a LS app.
Following "bric/brac" code is doing exactly what you want:
class Program { static void Main(string[] args) { ServiceReference2.ApplicationData ctx = new ServiceReference2.ApplicationData(new Uri(@"http://localhost:23286/ApplicationData.svc")); var customers = ctx.Customers; //ctx.Credentials = new NetworkCredential("test", "test"); foreach (var item in customers) { Console.WriteLine(item.LastName); } //adding a new customer Customer c = new Customer { LastName = "customer by command line process", FirstName="FindMeLater" }; ctx.AddToCustomers(c); ctx.SaveChanges(); Customer existing = ctx.Customers.Where(cc => cc.FirstName == "FindMeLater").First(); existing.LastName = "now we update again"; ctx.UpdateObject(existing); ctx.SaveChanges(); Console.Read(); } }
paul van bladel
- Proposed as answer by Huy Nguyen MSFTMicrosoft employee Wednesday, June 26, 2013 9:58 PM
- Marked as answer by Angie Xu Monday, July 8, 2013 2:17 AM
Thursday, June 13, 2013 7:08 PM