EnsureDeleted does not reset "identity" columns for InMemory database provider
See original GitHub issueWhen unit testing using the InMemory provider, we noticed that using the context.Database.EnsureDeleted() method does successfully “delete” the database (and its data), but it does not reset the values that are assigned to new records for integer id columns.
This is using 7.0.0-rc1-final.
Thanks, Kevin
Repro:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class MyContext : DbContext
{
public MyContext()
{
}
public MyContext(DbContextOptions options) : base(options)
{
}
public virtual DbSet<Person> People { get; set; }
}
[Fact]
public void ShowFailure()
{
var db1 = GetMyDb();
var zach1 = db1.People.Where(p => p.Id == 1).FirstOrDefault();
Assert.NotNull(zach1); // works
var db2 = GetMyDb();
var zach2 = db2.People.Where(p => p.Id == 1).FirstOrDefault();
Assert.NotNull(zach2); // fails (because the DB is on 3 and 4 now)
}
private static MyContext GetMyDb()
{
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
// This is the magic line
optionsBuilder.UseInMemoryDatabase();
var db = new MyContext(optionsBuilder.Options);
db.Database.EnsureDeleted();
db.People.Add(
new Person
{
Name = "Zach"
});
db.People.Add(
new Person
{
Name = "George"
}
);
db.SaveChanges();
return db;
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
EnsureDeleted does not reset "identity" columns for ...
EnsureDeleted () method does successfully "delete" the database (and its data), but it does not reset the values that are assigned to new...
Read more >How can I reset an EF7 InMemory provider between unit ...
The following call will clear the in-memory datastore. _context.Database.EnsureDeleted();. Be careful that you'll never accidentally run ...
Read more >Generated Values - EF Core
How to configure value generation for properties when using Entity Framework Core.
Read more >Application Programming Interface
This extension method sets the provider and database connection configuration. Developers can set any connection string attributes that are available in ODP.NET ...
Read more >How can I reset an EF7 InMemory provider between unit tests?
I would go with a combination of both answers. If tests run in parallel, you could have a database being deleted while you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This complicates using InMemory provider for unit testing - which I thought was one of the major points of the provider in the first place.
I want to be able to test an ASPNET core mvc app using EF and inmemory. I’m finding that very difficult to do because of the fact there is no way to really reset an inmemory database. There is also no easy way I have found to provide a new service provider into a TestServer via its Startup class. I shouldn’t have to completely change how ASPNET core configures EF to do this, right? I had hopes that specifying a string to inmemory database would let me force it to use a new implementation, so for instance I tried using a new guid there, but that still didn’t work and the id values still stepped on one another.