question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Database was initialized failed in unit test when there were two databases

See original GitHub issue

Hi,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:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
leewangqcommented, Jan 17, 2017

@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:

public virtual void Initialize()
{
	Database.CreateIfNotExists();// add for unit test
	Database.Initialize(false);
	this.SetFilterScopedParameterValue(AbpDataFilters.MustHaveTenant, AbpDataFilters.Parameters.TenantId, AbpSession.TenantId ?? 0);
	this.SetFilterScopedParameterValue(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, AbpSession.TenantId);
}

Finally, my unit test have worked well .

0reactions
hikalkancommented, Jan 17, 2017

Thank you @leewangq for sharing your solution.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found