我正在使用 RIA 服务开发 Silverlight 3 应用程序。我已经让应用程序运行了,但由于某种原因,它只是读取数据,而不是提交更改。
我见过的大多数在线示例都使用 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();
}