-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Seed InMemory database automatically (i.e. without calling EnsureCreated) #11666
Comments
@jsacapdev Currently there needs to be a call to |
Keep it... |
@ErikEJ More details please... |
Seems logical that this only happens during migrations being applied |
@ajcvickers thanks for the guidance. |
Putting this on the backlog for now to consider for later release based on feedback. |
When should one call |
@anorborg You can just call it before using the context: using (var context = new MyDbContext())
{
context.Database.EnsureCreated();
...
} |
I vote for explicitly calling Imagine having a test which needs the
If seeding is implicitly called then this may lead to unexpected results. |
Is this still valid? Do you still have call EnsureCreated()? |
@victormarante Yes. |
Why? Doesn't makes sense to me, that EnsureCreated needs to be called explicitly. |
@JuergenGutsch Seeding is performed as part of database initialization. To trigger database initialization you need to call EnsureCreated(). Relational providers can also use migrations tools to do this, but otherwise it works the same way. |
Hi @AndriySvyryd |
We discussed this again as part of planning and agreed that requiring a call to EnsureCreated is desirable here since this type of seeding is inherently associated with the database creation and migration. |
Hello All, I have been utilizing this in my tests and it has been working great. However, I realized today after making additional tests that it seems that when So it would seem now that there is a race condition that the first test that calls with Is this expected behavior? I can verify that all four contexts are scoped appropriately and only get created once per test, each with the I am still learning the ropes here, so there might be user error as well, but wanted to do a quick sanity check to explore options if not before diving in. |
@Mike-E-angelo By default, EF builds a single model per context type and then caches the model. This is because model building can be slow. |
Thank you for the confirmation, @ajcvickers. I suspected as such and have adjusted my initialization components accordingly, making use of the Upsert package found here. 👍 |
I still have no idea how to seed existing data into context,
|
In "Announcing Entity Framework Core 2.1 Preview 2" it states "Data seeding now works with in-memory databases."
For my unit tests, the In-Memory store does not get data populated with the data set up in OnModelCreating. So in the unit test method
Seed_It
i get nothing returned back. Am I doing something incorrectly?Steps to reproduce
BloggingContext.cs
ValuesController.cs
ValuesControllerTests.cs
Further technical details
EF Core version: 2.1.0-preview2-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows Server 2016
IDE: Visual Studio Code
Complete code listing: https://github.com/jsacapdev/asp-net-core-2-1-preview.git
The text was updated successfully, but these errors were encountered: