locked
Updating Foreign Key relationships RRS feed

  • Question

  • I have a foreign key relationship Users can have many CRMWorkQueueItems. Is there an easier way of changing the foreign key relationship than creating a User object and assigning it to the CRMWorkQueueItems. All's I want to do is

    CRMWorkQueueItems.UserName = "Test". This is from the examples on Microsoft's site and seems very cumbersome.

     

    Dim WorkQueueItem As CRMWorkQueueItems = GetWorkQueueItemByID(_WorkQueueItemId)
    User = objUser.GetUserByUsername(_Username)

    ctxPeninsula.AttachTo("AdviceUsers", User)
    ctxPeninsula.SetLink(WorkQueueItem, "AdviceUsers", User)
    WorkQueueItem.AdviceUsers = User

    ctxPeninsula.UpdateObject(WorkQueueItem)
    ctxPeninsula.SaveChanges()

    Wednesday, December 3, 2008 10:10 AM

Answers

  •  

    Here's the code you mentioned:

     

    ctxPeninsula.AttachTo("AdviceUsers", User) // You do not need to do this, if GetUserByUsername quires the user using the underlying context

    ctxPeninsula.SetLink(WorkQueueItem, "AdviceUsers", User)
    WorkQueueItem.AdviceUsers = User

    ctxPeninsula.UpdateObject(WorkQueueItem) // For updating the links, you do not need to call UpdateObject.

    ctxPeninsula.SaveChanges()

     

    Since relationships are first class concept in the EDM model (which astoria data services follows), you need to call SetLink or AddLink to specify that you are updating a relationship. The line "WorkQueueItem.AdviceUsers = User" is just for making the client side object model consistent with the server. If you are not planning to use the context, then you need not do this also. SetLink followed by SaveChanges should be enough to make the right changes in the server.

     

    Plus remember, foreign keys are very database specific. The way ado .net data services can be used is over any data source (maybe a xml file or txt file in the server, if you can expose IUpdatable for that data source). Hence we need to stay away from database specific stuff and follow the data model.

     

    Hope this helps.

     

    Thanks

    Pratik

    Wednesday, December 3, 2008 6:30 PM
    Moderator