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 in which there are sections related to the device, and provision for adding Instrunctions to the device.
If the device is new, then it this.context.Devices.Add(device)
works fine.
but if I want to edit the device. I find it on Id. It is populated. and if I change an individual property, then it works fine. but I want to update the whole device at once, which does not happen.
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();
if (newDevice == null)
{
this.context.Device.Add(device);
return await this.context.SaveChangesAsync() > 0;
}
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 instructions. for example, Previously, a device called "D22" has 3 instructions (a1, a2, a3).
"D22_1", : a1, a3 ( ), a2 (). b1 a4 , a1, a2, b1, b4 D22_1.
: -
, .
1) D22, , , .
device.Name="aaaaa";
this.context.savechanges();
. .
2)
this.context.Entry(device).State = EntityState.Modified;
. , , : " " " " "" ", . - , .."
, -. , , EF. , EF (.. / ).
.