2011-04-22 125 views
1

我正在使用使用Silverlight RIA Services的应用程序。我对这项技术不是很熟悉。目前,我已经加载了用户的内容。用户可能有也可能没有地址。地址是CLR对象,如果用户未提供地址,则该对象可能为空。如果他们有,地址包含街道,城市,州相关信息。该地址在我的视图模型中设置为属性。我的UI在我的视图模型中以两种方式绑定到Address属性的属性。Silverlight RIA Services - 提交更改/更新

当用户在我的页面上单击“保存”时,我想要将此地址插入或更新到我的数据库中。在努力做到这一点,我有:

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

我注意到,没有什么是得到通过使用SQL事件探查器发送到数据库。如何使用RIA服务获取数据库的更改?

谢谢!

+0

你能分享你的用户和地址类的定义是什么? – 2011-04-22 20:42:06

+0

您应该提供有关您的设置的更多上下文。定义,你如何加载数据等 – AbdouMoumen 2011-04-22 21:02:45

回答

1

Ed的例子当然是解决你的需求的好方法,但我建议你的方法在Silverlight涉及RIA服务使用回调操作:

// 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(); 
            } 
           }; 
0

我假设你的模式是服务器上的东西定义如下

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; } 
} 

和你DomainService允许地址被插入/更新。

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

在客户端,当您添加一个新的Address,您的视图模型将其设置的用户。

this.User.Address = new Address(); 

这将导致InsertAddress方法上呼吁

this.DomainContext.SubmitChanges(); 

您的域名服务,如果Address已经存在,则

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

导致UpdateAddress方法对您的域名服务,称为

this.DomainContext.SubmitChanges(); 

如果您可以共享更多的代码,我可以清理我的示例以更直接地应用您的问题。

相关问题