Database was initialized failed in unit test when there were two databases
See original GitHub issueHi,hikalkan I have used Abp 1.0.0. and paid Abp project 1.13. I have two databases in my project and I have add the generic method UsingDbContext in AppTestBase like that:
protected void UsingDbContext<TDbContext>(int? tenantId, Action<TDbContext> action)
where TDbContext : AbpDbContext
{
using (UsingTenantId(tenantId))
{
using (var context = LocalIocManager.Resolve<TDbContext>())
{
context.DisableAllFilters();
action(context);
context.SaveChanges();
}
}
}
when I run the test,the error was throw like below: {“Table ‘AndCargoes’ was not found. The database was probably not initialized.\r\n\r\nIf using CodeFirst try to add the following line:\r\ncontext.Database.CreateIfNotExists()”}
I found that DbContextA and DbContextB which are corresponding to the tow database shared same DbConnection instance because the DbConnection was only created once by using Effort.EF6 . If DbContextA and DbContextB have diffrent DbConnection instance, the error will not exsit.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
SQL Server test databases fail when multiple ...
I'm working on an intermittent issue experienced with: Entity Framework Core 3.1, C#; SQL Server 2019; Unit tests (xUnit). Tests:
Read more >Different db for testing in Django?
I have two databases: one I'd like to use for unit tests, and one for everything else. Is it possible to configure this...
Read more >The argument against clearing the database between tests ...
Truncating the DB between every test is indeed horrifically slow. ... Querying a live database during unit tests fails on both accounts.
Read more >How do you guys deal with unit testing against a database?
I find it so annoying to do unit tests because when I run one unit test and changes the data in the database,...
Read more >Entity Framework Core - database initialization with Unit Test
I've recently been presented a concept of initializing the database (creating or re-creating it) with Unit Test method.
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
@hikalkan @iyhammad this error was caused by wrong data .After I correct the test data, it threw new error: The database was probably not initialized.\r\n\r\nIf using CodeFirst try to add the following line:\r\ncontext.Database.CreateIfNotExists()"
I have updated AbpDbContext’s method Initialize() like that:
Finally, my unit test have worked well .
Thank you @leewangq for sharing your solution.