Hi,
Sharing a basic example using which we can create Parent entity and the related entity records in the same operation\
http://msdn.microsoft.com/en-us/library/gg309638.aspx
Check out this helpful post
http://inogic.blogspot.com/2011/12/odata-properties-and-methods-explored.html
public MainPage() { InitializeComponent(); } GGContext context; private void UserControl_Loaded(object sender, RoutedEventArgs e) { var orgServiceUrl = "http://servername/orgname/XRMServices/2011/OrganizationData.svc"; // initialize the context context = new GGContext(new Uri(orgServiceUrl)); // using client http stack context.HttpStack = System.Data.Services.Client.HttpStack.ClientHttp; context.UseDefaultCredentials = false; context.Credentials = new NetworkCredential("administrator", "password", "domain"); // create contact record Contact contact = new Contact(); contact.LastName = "Rana"; contact.FirstName = "Nisha"; // create two tasks and one email activity var myTask1 = new Task(); myTask1.Subject = "Task1 created at " + DateTime.Now.ToLongTimeString(); var myTask2 = new Task(); myTask2.Subject = "Task2 created at " + DateTime.Now.ToLongTimeString(); var myEmail = new Email(); myEmail.Subject = "Email created at " + DateTime.Now.ToLongTimeString(); // add the tasks and email to their respective set context.AddToTaskSet(myTask1); context.AddToTaskSet(myTask2); context.AddToEmailSet(myEmail); // add contact to be created to the contact set context.AddToContactSet(contact); // add the related records contact.Contact_Tasks.Add(myTask1); contact.Contact_Tasks.Add(myTask2); contact.Contact_Emails.Add(myEmail); // add the link // http://inogic.blogspot.com/2011/12/odata-properties-and-methods-explored.html context.AddLink(contact, "Contact_Tasks", myTask1); context.AddLink(contact, "Contact_Tasks", myTask2); context.AddLink(contact, "Contact_Emails", myEmail); context.BeginSaveChanges(CreateContactHandler, contact); } private void CreateContactHandler(IAsyncResult result) { // in the call back method call the EndSaveChanges method // it returns DataServiceResponse object. // we can get the error information from this object in case an operation fails context.EndSaveChanges(result); // id of the created contact record Guid createdContactGuid = ((Contact)result.AsyncState).ContactId; }
Hope it helps
I tried this method when trying to create a new quote from an existing quote. Basically a quote copy process and I get an error “the context is already tracking the entity”. can you help?
LikeLike