【问题标题】:Silverlight RIA Services - Submitting Changes / UpdatingSilverlight RIA 服务 - 提交更改/更新
【发布时间】:2011-04-22 18:21:38
【问题描述】:

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

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

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

我注意到使用 SQL Profiler 没有向数据库发送任何内容。如何使用 RIA 服务更改数据库?

谢谢!

【问题讨论】:

  • 你能分享一下你的用户和地址类的定义吗?
  • 您应该提供有关您的设置的更多上下文。定义,您如何加载数据等。

标签: c# silverlight


【解决方案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();
                                    }
                                };

【讨论】:

    【解决方案2】:

    我假设您的模型在服务器上定义为类似

    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 时,您的 ViewModel 会将其设置在用户上。

    this.User.Address = new Address();
    

    这会在您的域服务上调用 InsertAddress 方法

    this.DomainContext.SubmitChanges();
    

    如果Address已经存在,那么

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

    导致在您的域服务上调用 UpdateAddress 方法

    this.DomainContext.SubmitChanges();
    

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

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多