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