【silverlight引用wcf数据服务 无法保存数据 求原因】
自定义SQL2008数据库 新建ado.net数据实体模型 然后添加 WCF数据服务 在用silverlight引用该服务
显示 数据的部分 全部可以使用 但是 无法保存修改或者新增的数据
以下是SilverlightSampleEntity.cs的部分主要内容 该文件是类库文件 不是silverlight的后置文件
这个例子 是仿照msdn上提供的做的 稍微有些修改 当执行添加函数的时候 AddArticle的时候会在
db.EndSaveChanges(result);报错 提示【处理此请求时出错】
msdn提示用 Dispatcher 但这里是 类库文件 使用不了
现在的情况就是 无法保存数据
SampleEntities db = new SampleEntities(new Uri("SampleWcf.svc", UriKind.Relative));//建立WCF//添加实体 public void AddArticle(article ArtileEntity) { db.AddObject("article",ArtileEntity);//添加 db.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, db);//保存 } private void OnChangesSaved(IAsyncResult result)//回调函数 { // Use the Dispatcher to ensure that the // asynchronous call returns in the correct thread. db = result.AsyncState as SampleEntities; db.EndSaveChanges(result); }
public partial class AddModefy : ChildWindow { SampleEntities db = new SampleEntities(new Uri("SampleWcf.svc", UriKind.Relative)); public AddModefy() { InitializeComponent(); } private void OKButton_Click(object sender, RoutedEventArgs e) { article entity = new article(); entity.title = titile.Text; entity.author = author.Text; entity.categotyid = 1; db.AddObject("article", entity); db.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, db); this.DialogResult = true; } private void OnChangesSaved(IAsyncResult result) { bool error = false; this.Dispatcher.BeginInvoke(() => { try { DataServiceResponse response = db.EndSaveChanges(result); foreach (ChangeOperationResponse changeResponse in response) { if (changeResponse.Error != null) error = true; } if (!error) { MessageBox.Show("数据更新成功"); } else { MessageBox.Show("数据更新失败"); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }); } private void CancelButton_Click(object sender, RoutedEventArgs e) { this.DialogResult = false; } }
SampleEntities dd = new SampleEntities(new Uri("SampleWcf.svc", UriKind.Relative)); private void btndelete_Click(object sender, RoutedEventArgs e) { var btn = sender as Button; string delid = btn.Tag.ToString(); int delidi = int.Parse(delid); article.article atcdel = DSC.Where(p => p.id == delidi ).SingleOrDefault(); dd.DeleteObject(atcdel); dd.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, null); } private void OnChangesSaved(IAsyncResult result) { bool error = false; this.Dispatcher.BeginInvoke(() => { try { DataServiceResponse response = db.EndSaveChanges(result); foreach (ChangeOperationResponse changeResponse in response) { if (changeResponse.Error != null) error = true; } if (!error) { MessageBox.Show("数据更新成功"); } else { MessageBox.Show("数据更新失败"); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }); }