我正在使用RIA Services开发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();
}
答案 0 :(得分:2)
删除[RequiresAuthentication]是否会改变行为?
要检查的另一件事可能是配置文件 - 特别是HttpHandler声明动词(GET,POST)。
(血腥的聚会消息列表 - 我将当天的3个消息限制作为newb):P
答案 1 :(得分:1)
是否有错误,当您调用SubmitChanges时没有任何反应?
以下是我的尝试:
我尝试添加一个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);
}
}
答案 2 :(得分:0)
感谢大家的帮助!我终于想出了让它发挥作用所需的条件。我不确定为什么到现在但这解决了这个问题。我将更新方法更改为以下内容:
[RequiresAuthentication()]
public void UpdateUserProfile(UserProfile currentProfile)
{
UserProfile originalProfile = this.ChangeSet.GetOriginal(currentProfile);
this.Context.UserProfiles.Attach(currentProfile, originalProfile);
}
呼。现在只是理解为什么它在抓取原始版本之前无法附加实体。
再次感谢大家,非常感谢!!