2009-07-22 69 views
1

我正在使用RIA服务开发Silverlight 3应用程序。我已经运行了应用程序,但由于某种原因,它只能读取数据,而没有进行更改。Silverlight 3 + RIA服务数据提交问题

我见过的大多数在线示例都使用Linq2Entities;我们使用LINQ2SQL(我们的数据模型是相当不错的,是不抽象。)

这里的服务的一个片段:

[EnableClientAccess] 
public class FooService : LinqToSqlDomainService<FooDataContext> 
{ 
    [RequiresAuthentication()] 
    public IQueryable<UserProfile> GetUserProfiles() 
    { 
     return this.Context.UserProfiles; 
    } 

    [RequiresAuthentication()] 
    public void InsertUserProfile(UserProfile profile) 
    { 
     this.Context.UserProfiles.InsertOnSubmit(profile); 
    } 

    [RequiresAuthentication()] 
    public void UpdateUserProfile(UserProfile currentProfile) 
    { 
     this.Context.UserProfiles.Attach(currentProfile, true); 
    } 

    [RequiresAuthentication()] 
    public void DeleteUserProfile(UserProfile profile) 
    { 
     this.Context.UserProfiles.Attach(profile, profile); 
     this.Context.UserProfiles.DeleteOnSubmit(profile); 
    } 
} 

下面是我使用的XAML的一个片段:

<dataControls:DataForm x:Name="_profileForm" AutoGenerateFields="False" CommandButtonsVisibility="Commit" AutoEdit="True" > 
       <dataControls:DataForm.EditTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <dataControls:DataField Label="Username"> 
           <TextBox Text="{Binding UserName, Mode=TwoWay}" /> 
          </dataControls:DataField> 

          <dataControls:DataField Label="First Name"> 
           <TextBox Text="{Binding FirstName, Mode=TwoWay}" /> 
          </dataControls:DataField> 

          <dataControls:DataField Label="Last Name"> 
           <TextBox Text="{Binding LastName, Mode=TwoWay}" /> 
          </dataControls:DataField> 

          <dataControls:DataField Label="Password"> 
           <PasswordBox Password="{Binding Password, Mode=TwoWay}"/> 
          </dataControls:DataField> 

          <!-- [Snip] --> 

          </dataControls:DataField> 
         </StackPanel> 
        </DataTemplate> 
       </dataControls:DataForm.EditTemplate> 
      </dataControls:DataForm> 

而这里的Silverlight的网页的一个片段:

public partial class Profile : Page 
{ 
    private FooContext _dataContext; 

    public Profile() 
    { 
     InitializeComponent(); 
     this._dataContext = new FooContext(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     LoadOperation<UserProfile> loadOperation = this._dataContext.Load<UserProfile>(this._dataContext.GetUserProfilesQuery()); 
     loadOperation.Completed += new EventHandler(this.LoadOperation_Completed); 
    } 

    private void LoadOperation_Completed(object sender, EventArgs e) 
    { 
     // Bind the RIA data to the controls 
     LoadOperation<UserProfile> loadOperation = sender as LoadOperation<UserProfile>; 
     this._profileForm.EditEnded += new EventHandler<DataFormEditEndedEventArgs>(ProfileForm_EditEnded); 
     this._profileForm.ItemsSource = loadOperation.Entities; 
     this._profileForm.CurrentIndex = 0; 
    } 

    private void ProfileForm_EditEnded(object sender, DataFormEditEndedEventArgs e) 
    { 
     this._dataContext.SubmitChanges(); 
    } 

回答

0

感谢大家的帮助!最后我想出了让这个工作起作用的方法。我不确定为什么,但是这解决了问题。我改变了更新的方法如下:

[RequiresAuthentication()] 
public void UpdateUserProfile(UserProfile currentProfile) 
{ 
    UserProfile originalProfile = this.ChangeSet.GetOriginal(currentProfile); 
    this.Context.UserProfiles.Attach(currentProfile, originalProfile); 
} 

。现在要理解为什么在抓取原始版本之前无法附加实体。

再次感谢大家,非常感谢!

1

是否有错误,不执行任何操作你打电话给SubmitChanges时发生?

这是我想尝试:

  1. 在服务器CRUD方法设置断点,以确保他们被调用。
  2. 确保您没有为任何值传递NULL,如that can cause a new instance to be created rather than an update of the existing entity
  3. 我想尝试添加一个OnSubmitCompleted事件来检查错误。示例代码(from this PDF):

    this._dataContext.SubmitChanges(OnSubmitCompleted, null); 
    
    private void OnSubmitCompleted(SubmitOperation so) 
    { 
         if (so.Error != null) 
         { 
           string message = so.Error.Message; 
           if (so.EntitiesInError.Any()) 
           { 
             message = string.Empty; 
             Entity entityInError = so.EntitiesInError.First(); 
             if (entityInError.Conflict != null) 
             { 
               EntityConflict conflict = entityInError.Conflict; 
               foreach (EntityConflictMember cm in 
                          conflict.MemberConflicts) 
               { 
                 message += string.Format( 
                   "Member '{0}' in conflict: Current: {1}, 
                         Original: {2}, Store: {3}", 
                   cm.PropertyName, cm.CurrentValue, 
                   cm.OriginalValue, cm.StoreValue); 
               } 
             } 
             else if (entityInError.ValidationErrors.Any()) 
             { 
               message += "\r\n" + 
                entityInError.ValidationErrors.First().Message; 
             } 
           } 
           MessageBox.Show(message, "Submit Failed", MessageBoxButton.OK); 
         } 
    } 
    
+0

1.我设置了断点,它正在调用更新方法。 2.没有空值正在传递(并且没有新的记录显示在数据库中)。 3.添加了该事件,因此.Error = null。 Silverlight客户端上的输出看起来很正常;我甚至添加了一个DataGrid,它反映了数据表单的变化。 我测试的服务下面只是为了确保它不是与DB写的问题,它的工作原理: 无功配置=(从Context.UserProfiles 型材,其中profiles.DBPrimaryKey == 1个 选择配置文件) 。第一(); profile.LastName =“已更改”; 上下文。的SubmitChanges(); – 2009-07-22 17:10:29

+0

我当前的怀疑是域服务上的UpdateUserProfile()方法,但我尝试了多个变体,没有任何更改。它被称为,但我已经证实了这一点。 – 2009-07-22 17:18:12

2

是否都去掉[RequiresAuthentication]改变行为?

另一件要检查的可能是配置文件 - 特别是HttpHandler声明动词(GET,POST)。

(血腥聚会的消息列表 - 我打我的3消息限制了一天,对于新手):P