locked
update association RRS feed

  • Question

  • how can i update an entity association with just the id of association?

     

    i have an entity Organization. i have an associaton on this object as OrganizationType. First i get an entity Organization with id 1. this organization entity has an organizationtype with id 5. i want to update this organization's organizationtype by just changing id field of organizationtype property of organization.

     

    how can i do that? i dont want to load organizationtype(5) and set it to the organization's organizationtype field.

    Saturday, January 12, 2008 9:41 PM

Answers

  • you should be able to instantiate a new OrganizationType object, set the key propety to 5 and then:

     

    DataServiceContext ctx = new DataServiceContext(...)

     

    // query for Organization org

     

    OrganizationType t = new OrganizationType();

    t.ID = 5;

     

    ctx.Attach("OrganizationTypes", t);

    ctx.AddSetBinding(org, "OrganizationType", t);

    ctx.SaveChanges();

     

     

     

     

    Wednesday, June 18, 2008 5:44 AM

All replies

  • You can set the EntityKey property on the EntityReference for the association.  So, for example, if Organization has a navigation property called OrganizationType, then there will also be a property called OrganizationTypeReference.  In that case the code would be something like this:

     

    Code Block
    organization.OrganizationTypeReference.EntityKey = new EntityKey("MyContainer.OrganizationTypeSet", "id", 5);

     

    Where you replace the container name, the entityset name for OrganizationType, the name of the property which is the key in OrganizationType and the value.

     

    - Danny

    Sunday, January 13, 2008 2:13 AM
  • there is no property like Entity key?  i generate my service by webdatagen tool.i think entitykey property exist on server side.

     

    organization.OrganizationTypeReference.EntityKey

    Sunday, January 13, 2008 6:58 AM
  • My answer was aimed at using the Entity Framework direclty.  Given that your question is about ado.net data services (aka Astoria), I'm going to move this thread to that forum--hopefully someone there will have a more specific answer for you.

     

    - Danny

     

    Monday, January 14, 2008 4:06 PM
  • you should be able to instantiate a new OrganizationType object, set the key propety to 5 and then:

     

    DataServiceContext ctx = new DataServiceContext(...)

     

    // query for Organization org

     

    OrganizationType t = new OrganizationType();

    t.ID = 5;

     

    ctx.Attach("OrganizationTypes", t);

    ctx.AddSetBinding(org, "OrganizationType", t);

    ctx.SaveChanges();

     

     

     

     

    Wednesday, June 18, 2008 5:44 AM