简体   繁体   中英

MVVM Ria Services. Update existing Entity

Every article i have found, shows how to add new items using a RIA. Doesnt anyone ever update existing data? It sure doesnt seem like it.

Scenario:

I am using a service from my VM to pull in my Entity. The entity is then edited using the VM from my view. I then want to save my entity back to the database. I dont want to add it. I don't want to delete it. I want to update it. I see the methods for that on the server side, but all I have is submit changes on my datacontext.

I have tried to clone the original entity,then attach the original entity and copy the current entity onto it

public void SaveDoctors(Doctors current, Doctors original, Action<SubmitOperation> callback)
        {
            var ctx = new RefereeDomainContext();

            ctx.Doctors.Attach(original);
            original = current;
            ctx.SubmitChanges(callback,null);


        }

This doesn't work either. I have gone through 10s of blogs today, and anything I have found is always about adding new entities, even when they claim they will show you how to update entities.

I could easily ship the entity to the server side and do it there, but I don't know that that is the correct form to take.

Where is the proper place to be updating Entities? I am using SL5, I am not sure how to tell you what version of RIA Services I am using.

Clarification

I retrieve my entity using a service call from my viewmodel:

Doctors doctors = GetService<IDoctors>.GetDoctor().FirstorDefault(d=>d.id=xxxx);

I push the doctor entity into my VM structure:

DoctorsVM=new DoctorsVM(doctors);

This holds a reference to the doctor that is edited through the properties of the viewmodel. Any Children are filled into ObservableCollections of the children objects.

When I am done with this particular entity and I am ready to save it:

GetService.UpdateCurrentDoctor(DoctorsVM.Doctor)//references the original entity.

This is where I lose it. If I do this: inside my method it doesnt work

var ctx=new DataContext;
ctx.Attach(currentDoctor);//attaches as unmodified

ctx.Add(currentDoctor);//creates a new Entity. Dont want that. 

ctx.AttachAsModified is only available on the Server side. ctx.UpdateDoctors is only available on the Server side.

This is where I get lost.

I come from a WPF/Desktop background, so maybe I am just lost in the whole async landscape. And I just need a push in the right direction.

The question a bit long for it ,so answer can a bit long :)

You should not clone entity if you really need it!

BackGround In entity model we have keys for each entities. If you inspect domain service you will see that all CRUD operations done by this key attribute. If you return a set from server side two entities have same key you will see one of them on client side.

Answer Its easy of updating a record. Just get entity using a DomainService class instance get item what you want to modify then submit it. That is all.

      //a line of unsafe-dummy code
      domService.GetDoctorByID("124..").FirstOrDefault().CitizenNumber='CN123';

      //domService class track changeset so it decide update
      domService.SubmitChanges();       
      //Update occurs by entity key i.e. DoctorID

I see "doctor" expression we also develop healthcare applications using silverlight & ria services. Good Luck!

It appears that you have to keep the datacontext alive for the duration of one session. I dont like this but, it is what it is. I am resetting the domaincontext on the next search from the application so that it doesnt get insanely huge.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM