EF how to update entity containing the List of entities

asp.net-mvc asp.net-mvc-5.2 c# entity-framework entity-framework-6

Question

I have a class Device as follows.

public class Device
{
    public string id { get; set; }
    public string name { get; set; }
    public List<Instructions> Instructions { get; set; }
}

Now I have a view which has sections related to device and provision to add Instrunctions to the device.

If the device is new then this.context.Devices.Add(device) is working fine.

but if I want to edit the device. I find it by Id. It is populated. and if I change the individual property then it is running fine. but I want to update the whole device at once which is not happening.

My sample code is as follows.

public async Task<bool> AddInstrument(Device device)
{
    try
    {
        var newDevice = this.context.Device
                        .Where(i => i.Id == device.Id).FirstOrDefault();//  i tried find,single or default also

        //adding new device
        if (newDevice == null)
        {
            this.context.Device.Add(device);
            return await this.context.SaveChangesAsync() > 0;
        }

        //if editing the device
        newDevice = device;
        return await this.context.SaveChangesAsync() > 0;
    }
    catch
    {
        throw;
    }
} 

what I want to achieve.

problem :- I want to update the corresponding device in the database. along with the instructions. e.g. Previously a device named "D22" has 3 instructions (a1,a2,a3).

but now same device has values name "D22_1" and now instructions are a1,a3(updated any field),a2(deleted). b1 & a4 are added so after editing the fields I have Instructions as a1,a2,b1,b4 for D22_1 .

My effort:-

What I have tried.

1) For main device D22 if I specify properties explicitly it is fine e.g.

device.Name="aaaaa";

this.context.savechanges();

reflects it in database. I have not tried this for updating Instructions in List.

2)I tried to use

this.context.Entry(device).State = EntityState.Modified;

but with various hit and trial I could not run it. The best I could get is this error " This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received .."

Please suggest me something. Moreover I want to know about various techniques related to updating entries with list in EF. I assume EF will automatically take care of the child object (i.e. addition/updation and deletion). In my case Instructions for device.

1
4
11/1/2015 6:10:39 AM

Popular Answer

Thanks to all of you. for showing me the way. My problem is resolved. Update/delete is now working fine. I will tune my solution for the add now. I took help of this "How to add/update child entities when updating a parent entity in EF"

0
5/23/2017 11:51:54 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow