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 the following class for devices.

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

I currently have a view with device-related parts and a feature to add instructions to the device.

When a device is brand-new,this.context.Devices.Add(device) works perfectly.

however, if I want to change the gadget. By ID, I locate it. It is inhabited. and if I adjust each individual property, everything works as it should. However, I'd like to update the entire device at once, which is not possible.

Here is a copy of my sample code.

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 hope to accomplish.

issue: I want to update the database entry for the relevant device. along with the guidelines. For instance, a device called "D22" has three instructions (a1,a2,a3).

however, the identical device now has values named "D22 1," and the current instructions are "a1" (update any field), "a3," and "a2" (deleted). After updating the fields, I now have Instructions for D22 1 as a1, a2, b1, & a4

My attempt:

what I've attempted.

1) If I explicitly define attributes for the primary device D22, such as

device.Name="aaaaa";

this.context.savechanges();

It appears in the database. I haven't tried using it to update the list of instructions.

2) In an effort to

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

But despite numerous attempts, I was unable to operate it. This error is the best I could muster "This can occur if any entities in the graph have incompatible key values when using the "Attach" method or when setting an entity's state to "Unchanged" or "Modified." This could be a result of certain entities being new and not having received."

Please provide a suggestion for me. Additionally, I'm interested in learning about other EF strategies for updating entries with a list. I expect that EF will handle the child object's addition, updating, and deletion on its own. In my situation, gadget instructions.

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

Popular Answer

I appreciate each and every one of you for guiding me. My issue has been handled. Update and deletion are now fully functional. I'll adjust my own solution right now. I used this "When changing a parent entity in EF, how to add/update child entities?" as assistance.

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