B Sharp
B Sharp

Reputation: 477

Updating data retrieved using RIA services custom methods

To date, everything I needed to do with RIA services was very simple and could be done using the code generated based on the domain model. However, I now need to have a custom method on the server side that either performs some logic or calls a stored proc in the database. I am able to write a custom server side method to get the filtered data from a table I need as follows:

public IQueryable<TIMESLOT> GetPermissableTimeslots(
    int roomID,
    int semesterID,
    Int16 year)
{
    return this.ObjectContext.TIMESLOTs.Where<TIMESLOT>(
        ts => 
            ts.ROOM_ID == roomID && 
            ts.SEMESTER == semesterID && 
            ts.YEAR == year
    );
}

(Note: I will eventually replace the LINQ query with a call to a stored proc once I get my current problems figured out)

Visual Studio generates GetPermissableTimeslotsQuery on the client which I am able to use to load the data as follows:

private void LoadPermissibleTimeSlots()
{
        this.domainContext.Load(
            domainContext.GetPermissableTimeslotsQuery(
                this.CurrentRoom.ID, 
                this.CurrentSemester.ID, 
                this.CurrentYear),
            (result) =>
            {
                this.Timeslots = result.Entities;
            }
            , null);
}

The problem I have now is adding and updating timeslots that the user changes in the client. Since the result I get is not an EntitySet and not associated directly with the TIMESLOTS table, I can't just update them in the resulting entity set and commit the changes. Do I need to attach the changed entities to an EntitySet associated with the TIMESLOTS table so that they can be committed, or do I need to add custom add and update methods on the server side to support this?

Upvotes: 1

Views: 510

Answers (1)

Scott Munro
Scott Munro

Reputation: 13586

The entities that are returned by the query should be attached automatically to the domain context. You should be able to modify them and have those changes propagated back to the server by calling the SubmitChanges method on the domain context.

To add new entities, you can add them to the domain context's Timeslots property.

Alternatively, if you create a relationship between the new entity and some other (does not need to be a Timeslot) then the new entity will be dragged into the domain context and included in the next call to SubmitChanges.

Upvotes: 1

Related Questions