locked
Convert earlybound to latebound (code inside) RRS feed

  • Question

  • How can I convert this: 

    // Create a new contact record; AdventureWorksCycleServiceContext context = new AdventureWorksCycleServiceContext (_serviceProxy); Contact contact = new Contact() { FirstName = "Pamela", LastName = "Brown", Address1_Line1 = "123 Easy St.", Address1_City = "Atlanta", Address1_StateOrProvince = "GA", Address1_PostalCode = "32254", Telephone1 = "425-555-5678" }; context.AddObject(contact); context.SaveChanges();

    To late binding?

    Friday, November 6, 2015 8:05 PM

Answers

  • the AddObject method of the OrganizationServiceContext class accept an Entity as parameter.

    You see often examples with early bound, but in fact the early bound classes are inherited from Entity

    so if you do something like

    AdventureWorksCycleServiceContext context = new AdventureWorksCycleServiceContext (_serviceProxy);
    
    Entity contact = new Entity("contact");
    contact["firstname"] = "Pamela";
    contact["lastname"] = "Brown";
    contact["address1_line1"] = "123 Easy St.";
    contact["address1_city"] = "Atlanta";
    contact["address1_stateorprovince"] = "GA";
    contact["address1_postalcode"] = "32254";
    contact["telephone1"] = "425-555-5678";
    
    context.AddObject(contact);
    context.SaveChanges();
    

    should work


    My blog: www.crmanswers.net - CRM Theme Generator

    Saturday, November 7, 2015 11:52 AM

All replies

  • Try below code.

      
    Entity contact = new Entity("contact");
    contact["firstname"] = "Pamela";
    contact["lastname"] = "Brown";
    contact["address1_line1"] = "123 Easy St.";
    contact["address1_city"] = "Atlanta";
    contact["address1_stateorprovince"] = "GA";
    contact["address1_postalcode"] = "32254";
    contact["telephone1"] = "425-555-5678";
    // Create an account record named Fourth Coffee.
    Guid contactid = _serviceProxy.Create(contact);


    • Proposed as answer by Kalim Khan Friday, November 6, 2015 8:16 PM
    • Edited by Kalim Khan Friday, November 6, 2015 8:17 PM
    Friday, November 6, 2015 8:15 PM
  • Thanks but I was experimenting with the addObject method.

    context.AddObject(contact); context.SaveChanges();


    Friday, November 6, 2015 8:26 PM
  • do you have specific needs to use the context instead creating directly the records using the IOrganiziationService?

    if not, Kalim code is correct


    My blog: www.crmanswers.net - CRM Theme Generator

    Saturday, November 7, 2015 3:57 AM
  • Just wanted to test the performance of some collections - Guido, can it be done (i.e. the early bound conversion)
    Saturday, November 7, 2015 11:36 AM
  • the AddObject method of the OrganizationServiceContext class accept an Entity as parameter.

    You see often examples with early bound, but in fact the early bound classes are inherited from Entity

    so if you do something like

    AdventureWorksCycleServiceContext context = new AdventureWorksCycleServiceContext (_serviceProxy);
    
    Entity contact = new Entity("contact");
    contact["firstname"] = "Pamela";
    contact["lastname"] = "Brown";
    contact["address1_line1"] = "123 Easy St.";
    contact["address1_city"] = "Atlanta";
    contact["address1_stateorprovince"] = "GA";
    contact["address1_postalcode"] = "32254";
    contact["telephone1"] = "425-555-5678";
    
    context.AddObject(contact);
    context.SaveChanges();
    

    should work


    My blog: www.crmanswers.net - CRM Theme Generator

    Saturday, November 7, 2015 11:52 AM
  • Thanks for that Guido - I am wondering why people use the addObject method instead of the service.create. What are the advantages?
    Monday, November 9, 2015 7:04 PM
  • organization service proxy exposes methods that directly performs the operation. Example when we call create() method operation will be performed in database.

    When we use OrganizationServiceContext operations are not transmitted to the server until we call SaveChanges().We can perform muiltiple operations like context.AddObject(contact) and  context.DeleteObject(contact) and perform the save operation at end. This is my understanding.
    Monday, November 9, 2015 9:05 PM
  • Thanks, that's what I always thought too. It would be great if someone else with a final view had an input
    Monday, November 9, 2015 9:41 PM