2
votes

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.

1

1 Answers

0
votes

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.

public void UpdateAddress(Address currentAddress)
        {
            this.DbContext.Addresses.AttachAsModified(currentAddress, this.ChangeSet.GetOriginal(currentAddress), this.DbContext);
        }

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.