开发者

How to update record by changing presentation model attributes?

I have tried to create an update method which should modify the database entry the following way:

        [Update]
        public void InsertMachinePM(MachinePM machinePM)
        {
            machine _machine = new machine();
            _machine.machine_id = machinePM.MachineID;
            int machineCategoryID = this.ObjectContext.machine_category.Where(so => so.title == machinePM.Category).FirstOrDefault().machine_category_id;
            _machine.machine_category_id = machineCategoryID;
            _machine.code = machinePM.Code;
            _machine.description = machinePM.Description;
            ObjectContext.machines.AddObject(_machine);
            ChangeSet.Associate(machinePM, _machine, UpdateMachinePMKeys);

        }

        private void UpdateMachinePMKeys(MachinePM machinePM, machine _machine)
        {
            machinePM.MachineID = _machine.machine_id;
        }

The code doesn't modify the existing record but adds a new one with modified attributes and new id.

I tried to change the la开发者_开发百科st two lines from the first method

ObjectContext.machines.AddObject(_machine);
ChangeSet.Associate(machinePM, _machine, UpdateMachinePMKeys);

with ObjectContext.SaveChanges();

but in this case the method doesn't modify the existing record either.

As I am still new with Silverlight and RIA I would appreciate if anyone could suggest a method to modify the existing record by changing presentation model values.

Thank you!


I've figured it out. The following code updates the existing record:

[Update]
    public void InsertMachinePM(MachinePM machinePM)
    {
        machine _machine = ObjectContext.machines.Where(c => c.machine_id == machinePM.MachineID).FirstOrDefault();
        int machineCategoryID = this.ObjectContext.machine_category.Where(so => so.title == machinePM.Category).FirstOrDefault().machine_category_id;
        _machine.machine_category_id = machineCategoryID;
        _machine.code = machinePM.Code;
        _machine.description = machinePM.Description;
        ObjectContext.SaveChanges();
        ChangeSet.Associate(machinePM, _machine, UpdateMachinePMKeys);

    }

    private void UpdateMachinePMKeys(MachinePM machinePM, machine _machine)
    {
        machinePM.MachineID = _machine.machine_id;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜