Не могу правильно обновить данные сущности
Не могу правильно обновить сущность, постояннно выскакивает исключение:
System.InvalidOperationException: "The instance of entity type 'Inventory' cannot be tracked because another instance with the same key value for {'Inventid'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values."
Что делаю не так, тоже понять не могу, обычный метод Update()
не работает!
var id = context.Inventories.Where(x => x.Inventid == Convert.ToInt32(dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString()))
.Select(z => z.Inventid).FirstOrDefault();
var fil = context.Filials.Where(x => x.Filialname == cbxFilial.Text)
.Select(z => z.Filialid).FirstOrDefault();
var invttype = context.Inventtypes.Where(x => x.Inventname == cbxTypename.Text)
.Select(z => z.Inventtypeid).FirstOrDefault();
var cabinet = context.Cabinets.Where(x => x.Cabinetname == cbxCabnum.Text)
.Select(z => z.Cabinetid).FirstOrDefault();
var service = context.Services.Where(x => x.Servicename == cbxService.Text)
.Select(z => z.Serviceid).FirstOrDefault();
var status = context.Statuses.Where(x => x.Statusname == cbxStatus.Text)
.Select(z => z.Statusid).FirstOrDefault();
Inventory inv = new()
{
Inventid = id,
Inventname = tbxName.Text,
Serialnum = tbxSerialnum.Text,
Datepov = dtpDatepov.Value.Date.ToUniversalTime(),
Datenextpov = dtpDatenextpov.Value.Date.ToUniversalTime(),
Filialid = (int)fil,
Inventtype = invttype,
Cabinetid = cabinet,
Serviceid = service,
Statusid = status,
Yearofman = dtpYearOfMan.Value.Date.ToUniversalTime()
};
context.Entry(inv).State = EntityState.Modified;
await context.SaveChangesAsync();