Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:45:38+00:00 2026-06-17T12:45:38+00:00

I am writing integration tests to verify the behaviour in my repositories. The problem

  • 0

I am writing integration tests to verify the behaviour in my repositories. The problem I run into is that I want to start with a clean database for every test. I managed to do so by writing the SetUp as follows:

    [SetUp]
    public void SetUp()
    {
        _applicationContext = new TestApplicationContext();

        _applicationContext.Database.Connection.Close();
        _applicationContext.Database.Delete();
        _applicationContext.Database.Create();

        _tenantRepository = new TenantRepository(_applicationContext);
        _userRepository = new UserRepository(_applicationContext);
    }

The TestApplicationContext sets the database name to TestDatabase.

This works fine until I want to check the actual database for the result of my test. Then I make a connection from MSSQL server to the same database, which won’t close until I either:

  • shut down MSSQL server
  • delete the database with the option “close all connections”

The only way I found is via SQL commands. Maybe it’s because of my n00b knowledge regarding MSSQL, but I was kinda hoping for a “close connection” button / option.

How can I close the connection to the database from MSSQL server?
Or, is there a way I can do this programmatically from C#?

UPDATE:

Maybe I wasn’t very clear. But the test SetUp fails after I opened MSSQL and viewed the contents of a table. This is because MSSQL also creates a connection to the TestDatabase, and my integration test is not able to close that connection. So I am looking for a solution that allows me to close the connection I created from MSSQL server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:45:39+00:00Added an answer on June 17, 2026 at 12:45 pm

    You can work around the connection problem if instead of dropping and re-creating whole database you just drop and re-create selected (or all tables).

    You could create little script that will do it for you in a way that you do not need to hard-code table names:
    http://www.apexure.com/blog/2010/07/29/delete-all-tables-in-sql-server-database/

    Alternatively, Julia Lerman in her book “Programming Entity Framework: Code First” mentiones this approach in more mature form, as incorporated as a custom database initializer:

    As well as writing your own custom initializers, you can also find
    initializers that other people have created. One example of this is
    available in the EFCodeFirst.CreateTablesOnly NuGet package. This
    initializer will allow you to drop and create the tables in an
    existing database, rather than dropping and creating the actual
    database itself. This is particularly useful if you are targeting a
    hosted database where you don’t have permission to drop or create the
    entire database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing integration tests.Now, before tests are run I want to setup the
I am writing some integration tests to verify the correctness of convert to PDF
I'm writing some integration tests for my application in Spring.I want to test some
I'm writing an integration tests for my database, and I've got one question. At
I'm writing unit tests (technically integration tests since I am connecting to a database)
As part of a suite of integration tests I am writing, I want to
I'm writing a tool to run a series of integration tests on my product.
I am writing a bunch of integration tests for a project. I want to
I have a problem with an integration test I am writing. I need to
In short I am writing a class handler to handle to database integration of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.