As part of my EF 6.1 to EF Core 2.0 migration I added a simple test to check if the concurrency tokens do work the same way. I noted, however, that is dependent on the underlying database provider: it works for SqlServer, but it does not for MS InMemory database.
The entity class is pretty simple:
public class AcademicTermDate
{
public int AcademicTermDateID { get; set; }
public DateTime StartDate { get; set; } //but no end date, because it's derived in controcc and rederived here.
public bool Deleted { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
The code that creates it is also trivial:
using (var context = _factory.CreateDbContext(null))
{
var term = new AcademicTermDate();
term.StartDate = new DateTime(2001, month, 1);
context.AcademicTermDate.Add(term);
context.SaveChanges();
}
What is interesting, if I use old plain Sql Server as per following code:
public MyContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<MyContext>();
var connectionString = "server=.\\sql2012;Database=CA15;Trusted_Connection=True;";
builder.UseSqlServer(connectionString);
return new MyContext(builder.Options);
}
it works as expected; on context.SaveChanges() I can see RowVersion to be populated.
If, however, I use the InMemory database provider, which seemed so tempting to be used for my tests, I can see a different behaviour: RowVersion remains populated with null value (i.e. not initialised at all).
For the latter, the factory is defined as:
public MyContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<MyContext>();
builder.UseInMemoryDatabase(databaseName: "InMemory");
return new MyContext(builder.Options);
}
Am I missing any vital setting for InMemory db I should provide? The difference seem odd and, honestly, quite disturbing.
All code targets .NET Core 2.0:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="4.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
</ItemGroup>
Any help greatly appreciated.