voglio aggiornare una riga in EF:Entity aggiornamento quadro fallimento
Case newCase = new Case(...);
dbContext.Entry(newCase).State = EntityState.Modified;
dbContext.SaveChanges();
Quando si aggiorna ho questo messaggio:
Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded
Non c'è concorrenza, dal momento che sto usando il db sul mio macchina.
Se vado a SQL Profiler per vedere la query che viene eseguito, la condizione è:
WHERE [Id] = @ p0 E [RowVersion] = @ p14;
in questa condizione, @ p0 è = 1 e @ p14 è NULL, esattamente come il record che voglio modificare. Ma se lancio manualmente questa query in Sql Server dopo aver rimosso la parte RowVersion della query, ha esito positivo, aggiornando 1 riga.
Quale può essere il problema e come posso risolverlo?
EDIT:
ho cercato di selezionare il record appena prima di aggiornarlo, ma l'errore è lo stesso:
IEnumerable<Case> cases = from c in dbContext.Cases where c.Id.ToString() == "1" select c;
Case cs = cases.SingleOrDefault();
dbContext.Entry(cs).State = EntityState.Modified;
dbContext.SaveChanges();
Ecco l'errore completo:
An exception of type 'Microsoft.Data.Entity.DbUpdateConcurrencyException' occurred in EntityFramework.Core.dll but was not handled in user code
Additional information: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
E qui è la query di Sql Profiler:
exec sp_executesql N'SET NOCOUNT OFF;
UPDATE [Caso] SET [Chiuso] = @ p1, [Commento] = @ p2, [ContactComment] = @ p3, [ContactId] = @ p4, [CreatedBy] = @ p5, [CreatedDateTime] = @ p6, [ Descrizione] = @ p7, [Email] = @ p8, [LastModifiedBy] = @ p9, [LastModifiedDateTime] = @ p10, [OpenedDateTime] = @ p11, [Telefono] = @ p12, [RowVersion] = @ p13, [SlaConsumedTime ] = @ p15, [SlaSuspended] = @ p16, [SlaTotalTime] = @ p17, [Stato] = @ p18, [Titolo] = @ p19 DOVE [Id] = @ p0 AND [RowVersion] = @ p14;
SELECT @@ ROWCOUNT; ', N' @ p0 int, @ p1 bit, @ p2 nvarchar (max), @ p3 nvarchar (max), @ p4 int, @ p5 int, @ p6 datetime2 (7), @ p7 nvarchar (4000), @ p8 nvarchar (4000), @ p9 int, @ p10 datetime2 (7), @ p11 datetime2 (7), @ p12 nvarchar (max), @ p13 varbinary (max), @ p14 varbinary (max), @ p15 time (7), @ p16 bit, @ p17 time (7), @ p18 int, @ p19 nvarchar (4000) ', @ p0 = 1, @ p1 = 0, @ p2 = NULL, @ p3 = NULL, @ p4 = 0, @ p5 = 1, @ p6 = '2015-09-23 09: 07: 55.7041023', @ p7 = N'y ', @ p8=N'[email protected]', @ p9 = 1, @ p10 = ' 2015-09-23 09: 50: 02.9934006 ', @ p11 =' 2015-09-23 09: 07: 55.6796028 ', @ p12 = NULL, @ p13 = NULL, @ p14 = NULL, @ p15 = '00: 00 : 00 ', @ p16 = 0, @ p17 = '00: 00: 00', @ p18 = 0, @ p19 = N'y '
l'entità 'Caso' ha sicuramente una proprietà' RowVersion', utilizzata per il controllo della concorrenza. Ma quando crei un 'Case' con' new', quella proprietà *** non ha *** il valore originale attualmente memorizzato nel database.Quindi la condizione WHERE non è riuscita, è necessario impostare manualmente i valori originali per 'RowVersion' con un valore recuperato dal database prima di chiamare' SaveChanges() '. – Hopeless
'dbContext.Cases.Add (newCase)' (si tratta di un nuovo oggetto, non di uno esistente) –
Si sta tentando di aggiornare un oggetto che non esiste nel database. – trailmax