WCF RIA 服务 - 使用命名更新时出错
我构建了一个非常简单的 Silverlight RIA 解决方案,在服务器端使用 EF4。我向 DomainService 添加了一个命名更新方法,但无法使用它。问题出在 ChangeSet.GetOriginal() 上。它返回 null :
[EnableClientAccess()]
public class StudentsDomainService : LinqToEntitiesDomainService<Model1Container>
{
[Update(UsingCustomMethod = true)]
public void MyMethod(Student stud, int a, int b)
{
stud.FirstName = (a*b).ToString();
var original = this.ChangeSet.GetOriginal(stud);
// original is null;
this.ObjectContext.StudentSet.AttachAsModified(stud, original); //Exception is thrown
}
.
.
.
}
这是 xaml 代码隐藏:
public partial class MainPage : UserControl
{
StudentsDomainContext ctx;
Student stud;
public MainPage()
{
InitializeComponent();
ctx = new StudentsDomainContext();
}
private void buttonGet_Click(object sender, RoutedEventArgs e)
{
ctx.Load<Student>(ctx.GetStudentSetQuery()).Completed += new EventHandler(MainPage_Completed);
}
void MainPage_Completed(object sender, EventArgs e)
{
var lo = (sender as LoadOperation<Student>);
stud = lo.Entities.First();
}
private void buttonChange_Click(object sender, RoutedEventArgs e)
{
stud.MyMethod(3, 6);
ctx.SubmitChanges();
}
}
值得注意的是,当我通过自动生成的 CRUD 使用简单的更新时,一切正常。
I built a very simple Silverlight RIA solution, with EF4 in the server side. I added a Named Update method to the DomainService, but I can't use it. The problem is with ChangeSet.GetOriginal(). It returns null :
[EnableClientAccess()]
public class StudentsDomainService : LinqToEntitiesDomainService<Model1Container>
{
[Update(UsingCustomMethod = true)]
public void MyMethod(Student stud, int a, int b)
{
stud.FirstName = (a*b).ToString();
var original = this.ChangeSet.GetOriginal(stud);
// original is null;
this.ObjectContext.StudentSet.AttachAsModified(stud, original); //Exception is thrown
}
.
.
.
}
this is the the xaml codebehind:
public partial class MainPage : UserControl
{
StudentsDomainContext ctx;
Student stud;
public MainPage()
{
InitializeComponent();
ctx = new StudentsDomainContext();
}
private void buttonGet_Click(object sender, RoutedEventArgs e)
{
ctx.Load<Student>(ctx.GetStudentSetQuery()).Completed += new EventHandler(MainPage_Completed);
}
void MainPage_Completed(object sender, EventArgs e)
{
var lo = (sender as LoadOperation<Student>);
stud = lo.Entities.First();
}
private void buttonChange_Click(object sender, RoutedEventArgs e)
{
stud.MyMethod(3, 6);
ctx.SubmitChanges();
}
}
It's important to note that when I using a simple Update via the auto generated CRUD everything works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题的简单答案是,您没有更改 silverlight 应用程序上的实体,因此没有任何内容发送到服务器,这就是为什么您无法获得原始的更多建议。
1.首先,我建议您使用Ria域服务中的标准更新功能,它看起来像下面的功能。然后在您的客户端,您可以自由更改实体的值,在您的情况下,您可以更改 silverlight 部分上的学生姓名。然后将更改全部提交到服务器。
2.更新动作在最新版本中已废弃,已被EntityAction取代。
3.如果你真的很想使用你的自定义方法,你可以尝试使用RoundtripOriginalAttribute,使用这个属性来注释你想要将其发送回服务器的所有属性。
The simple answer for your question, you didn't change the entity on your silverlight application, thus nothing get send to server, that's why your can't get the original a few more suggestions.
1.First of all, I would suggest you use the standard update function in Ria Domain service, which looks like something like the following function. Then on your client side, you are free to change the entity's value, in your case, you can change the student's name on your silverlight part. then submit the changes to server all together.
2.Update action is obsolete in the latest version , it has been replaced with EntityAction.
3.If you really really want to use your custom method , you can try to use RoundtripOriginalAttribute, use this attribute to annotate all the properties that you want send it back to server.