Ho un modello con una colonna Timestamp (Concurrency Token). Sto provando a scrivere un test di integrazione in cui controllo che funzioni come previsto ma senza successo. Il mio test sembra seguito
Qual è la ragione per questo? Ha qualcosa a che fare con Timestamp come valore generato da un database che fa ignorare le modifiche apportate ad EF dal livello aziendale?
L'applicazione di prova completa può essere trovata qui: https://github.com/Abrissirba/EfTimestampBug
public class BaseModel
{
[Timestamp]
public byte[] Timestamp { get; set; }
}
public class Person : BaseModel
{
public int Id { get; set; }
public String Title { get; set; }
}
public class Context : DbContext
{
public Context()
{}
public Context(DbContextOptions options) : base(options)
{}
public DbSet<Person> Persons{ get; set; }
}
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "7.0.0-rc1-16348")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("EFTimestampBug.Models.Person", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<byte[]>("Timestamp")
.IsConcurrencyToken()
.ValueGeneratedOnAddOrUpdate();
b.Property<string>("Title");
b.HasKey("Id");
});
}
// PUT api/values/5
[HttpPut("{id}")]
public Person Put(int id, [FromBody]Person personDTO)
{
// 7
var person = db.Persons.SingleOrDefault(x => x.Id == id);
person.Title = personDTO.Title;
person.Timestamp = personDTO.Timestamp;
db.SaveChanges();
return person;
}
[Fact]
public async Task Fail_When_Timestamp_Differs()
{
using (var client = server.CreateClient().AcceptJson())
{
await client.PostAsJsonAsync(ApiEndpoint, Persons[0]);
// 1
var getResponse = await client.GetAsync(ApiEndpoint);
var fetched = await getResponse.Content.ReadAsJsonAsync<List<Person>>();
Assert.True(getResponse.IsSuccessStatusCode);
Assert.NotEmpty(fetched);
var person = fetched.First();
// 2
var fromDb = await db.Persons.SingleOrDefaultAsync(x => x.Id == person.Id);
// 3
fromDb.Title = "In between";
// 4
await db.SaveChangesAsync();
// 5
person.Title = "After - should fail";
// 6
var postResponse = await client.PutAsJsonAsync(ApiEndpoint + person.Id, person);
var created = await postResponse.Content.ReadAsJsonAsync<Person>();
Assert.False(postResponse.IsSuccessStatusCode);
}
}
// generated sql - @p1 has the original timestamp from the entity and not the assigned and therefore the save succeed which was not intended
exec sp_executesql N'SET NOCOUNT OFF;
UPDATE[Person] SET[Title] = @p2
OUTPUT INSERTED.[Timestamp]
WHERE [Id] = @p0 AND[Timestamp] = @p1;
',N'@p0 int,@p1 varbinary(8),@p2 nvarchar(4000)',@p0=21,@p1=0x00000000000007F4,@p2=N'After - should fail'
Modifica 4 - Correzione
Ho sentito di nuovo un membro sul sito di GitHub, il numero 4512 . Devi aggiornare il valore originale sull'entità. Questo può essere fatto in questo modo.
var passedInTimestamp = new byte[] { 0, 0, 0, 0, 0, 0, 0, 120 }; // a hard coded value but normally included in a postback
var entryProp = db.Entry(person).Property(u => u.Timestamp);
entryProp.OriginalValue = passedInTimestamp;
Ho aggiornato il test dell'unità originale che ha avuto esito negativo nel caso in cui tu e io non DbUpdateConcurrencyException
potuto DbUpdateConcurrencyException
, ora funziona come previsto.
Aggiornerò il ticket GitHub per chiedere se possono apportare una modifica in modo che lo sql sottostante che viene generato utilizza il nuovo valore anziché il valore originale quando la colonna è contrassegnata come Timestamp
o IsConcurrencyToken
modo che si comporti in modo simile alle versioni precedenti di Entity Framework.
Per ora anche se questo sembra essere il modo di farlo con le entità distaccate.
Modifica # 3
Grazie, mi sono perso. Dopo aver eseguito di nuovo il debug di nuovo, capisco perfettamente il problema, anche se non perché si sta verificando. Probabilmente dovremmo prendere Web API fuori da esso, meno parti in movimento e non credo che esista una dipendenza diretta tra EF Core e Web API. Ho riprodotto il problema con i seguenti test che illustrano il problema. Sono riluttante a chiamarlo un bug, perché forse la convenzione per forzare l'EF Core a usare il valore di timestamp
passato è cambiata dall'EF6.
Ho creato un set completo di codice minimo funzionante e ho creato un problema / domanda sul sito GitHub del progetto. Includerò il test ancora una volta sotto per riferimento. Non appena avrò notizie, risponderò a questa risposta e ti farò sapere.
dipendenze
DDL
CREATE TABLE [dbo].[Person](
[Id] [int] IDENTITY NOT NULL,
[Title] [varchar](50) NOT NULL,
[Timestamp] [rowversion] NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
INSERT INTO Person (title) values('user number 1')
Entità
public class Person
{
public int Id { get; set; }
public String Title { get; set; }
// [Timestamp], tried both with & without annotation
public byte[] Timestamp { get; set; }
}
Db Context
public class Context : DbContext
{
public Context(DbContextOptions options)
: base(options)
{
}
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().HasKey(x => x.Id);
modelBuilder.Entity<Person>().Property(x => x.Id)
.UseSqlServerIdentityColumn()
.ValueGeneratedOnAdd()
.ForSqlServerHasColumnName("Id");
modelBuilder.Entity<Person>().Property(x => x.Title)
.ForSqlServerHasColumnName("Title");
modelBuilder.Entity<Person>().Property(x => x.Timestamp)
.IsConcurrencyToken(true)
.ValueGeneratedOnAddOrUpdate()
.ForSqlServerHasColumnName("Timestamp");
base.OnModelCreating(modelBuilder);
}
}
Test unitario
public class UnitTest
{
private string dbConnectionString = "DbConnectionStringOrConnectionName";
public EFTimestampBug.Models.Context CreateContext()
{
var options = new DbContextOptionsBuilder();
options.UseSqlServer(dbConnectionString);
return new EFTimestampBug.Models.Context(options.Options);
}
[Fact] // this test passes
public async Task TimestampChangedExternally()
{
using (var db = CreateContext())
{
var person = await db.Persons.SingleAsync(x => x.Id == 1);
person.Title = "Update 2 - should fail";
// update the database manually after we have a person instance
using (var connection = new System.Data.SqlClient.SqlConnection(dbConnectionString))
{
var command = connection.CreateCommand();
command.CommandText = "update person set title = 'changed title' where id = 1";
connection.Open();
await command.ExecuteNonQueryAsync();
command.Dispose();
}
// should throw exception
try
{
await db.SaveChangesAsync();
throw new Exception("should have thrown exception");
}
catch (DbUpdateConcurrencyException)
{
}
}
}
[Fact]
public async Task EmulateAspPostbackWhereTimestampHadBeenChanged()
{
using (var db = CreateContext())
{
var person = await db.Persons.SingleAsync(x => x.Id == 1);
person.Title = "Update 2 - should fail " + DateTime.Now.Second.ToString();
// This emulates post back where the timestamp is passed in from the web page
// the Person entity attached dbcontext does have the latest timestamp value but
// it needs to be changed to what was posted
// this way the user would see that something has changed between the time that their screen initially loaded and the time they posted the form back
var passedInTimestamp = new byte[] { 0, 0, 0, 0, 0, 0, 0, 120 }; // a hard coded value but normally included in a postback
//person.Timestamp = passedInTimestamp;
var entry = db.Entry(person).Property(u => u.Timestamp);
entry.OriginalValue = passedInTimestamp;
try
{
await db.SaveChangesAsync(); // EF ignores the set Timestamp value and uses its own value in the outputed sql
throw new Exception("should have thrown DbUpdateConcurrencyException");
}
catch (DbUpdateConcurrencyException)
{
}
}
}
}
Microsoft ha aggiornato il proprio tutorial per questo nella gestione dei conflitti di concorrenza - EF Core con il tutorial ASP.NET Core MVC . Specifica in particolare quanto segue in merito agli aggiornamenti:
Prima di chiamare
SaveChanges
, devi inserire il valore della proprietàRowVersion
originale nella raccoltaOriginalValues
per l'entità.
_context.Entry(entityToUpdate).Property("RowVersion").OriginalValue = rowVersion;
Quindi, quando Entity Framework crea un comando SQL UPDATE, tale comando includerà una clausola WHERE che cerca una riga con il valore
RowVersion
originale. Se nessuna riga è interessata dal comando UPDATE (nessuna riga ha il valoreRowVersion
originale), Entity Framework genera un'eccezioneDbUpdateConcurrencyException
.