user687554
user687554

Reputation: 11151

Silverlight RIA Services - Submitting Changes / Updating

I am working with an application that uses Silverlight RIA Services. I am not very familiar with this technology. Currently, I have loaded the contents of a user. The user may or may not have an Address. An Address is a CLR object that may be null if a user hasn't provided their address. If they have, the address contains street, city, state related info. This address is set to property in my view model. My UI does two way binding to the properties of the Address property in my view model.

When a user clicks "Save" on my page, I want this Address to get inserted or updated into my database. In an effort to do this, I have:

this.DomainContext.SubmitChanges(); // DomainContext is initialized in the constructor of my view model.

I have noticed that nothing is getting sent to the database by using SQL Profiler. How do I get changes to the database with RIA services?

Thanks!

Upvotes: 1

Views: 1900

Answers (2)

Keith Adler
Keith Adler

Reputation: 21178

Ed's example is certainly a great way to address your need, but I recommend that you approach operations in Silverlight involving RIA Services using callbacks:

// Save
            SubmitOperation so = dataContext.SubmitChanges();

            // Callback
            so.Completed += (s, args) =>
                                {
                                    // Error?
                                    if (so.HasError)
                                    {
                                        // Message
                                        MessageBox.Show(string.Format("The following error has occurred:\n\n{0}", so.Error.Message));

                                        // Set
                                        so.MarkErrorAsHandled();
                                    }
                                };

Upvotes: 1

Ed Chapel
Ed Chapel

Reputation: 6932

I am assuming your model is defined on the server as something like

public class User
{
    [Key]
    public int? UserID { get; set; }

    /* Other properties */

    [Association("User_1-1_Address", "UserID", UserID", IsForeignKey = false)]
    [Include]
    public Address Address { get; set; }
}

public class Address
{
    [Key]
    public int? UserID { get; set; }

    /* Other properties */


    [Association("User_1-1_Address", "UserID", UserID", IsForeignKey = true)]
    [Include]
    public User User{ get; set; }
}

and your DomainService allows a Address to be inserted/updated.

public void InsertAddress(Address address) { ... }
public void UpdateAddress(Address address) { ... }

On the client, when you add a new Address, your ViewModel would set it on the User.

this.User.Address = new Address();

This results in the InsertAddress method on your domain service called upon

this.DomainContext.SubmitChanges();

If the Address already exists, then

this.User.Address.City = "Oz";

results in the UpdateAddress method on your domain service called upon

this.DomainContext.SubmitChanges();

If you can share more of your code, I can clean up my examples to apply more directly to your problem.

Upvotes: 0

Related Questions