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 415539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:23:28+00:00 2026-05-12T18:23:28+00:00

In my project I want to run some unit tests on the DAL layer

  • 0

In my project I want to run some unit tests on the DAL layer that is using EntityFramework. I’m creating from scrips a new database before each run of the tests (in order to have always the same initial data when doing the tests). At the end of the tests, this database is dropped, (all is made automatically with the help of [ClassInitialize()] and [ClassCleanup()] attributes.

The generated database always has a different name, something like TestDB-2009-01-31–12-00-00, in order not to conflict with the test databases of my collegues.

The actual problem that I have is that I did not find yet a way to tell EntityFramework to connect to the generated database (the name is generated at runtime). Right now it connects to the connection string specified in the app.config file, which is normal, of course. And because I’m doing these tests, I’m looking for something that can be done from outside the DAL dll (without setting anything on the EF context directly).

Any help is greatly appreciated.

Thanks.

  • 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-05-12T18:23:28+00:00Added an answer on May 12, 2026 at 6:23 pm

    When you create the ObjectContext, you will need to use the constructor overload that takes a ConnectionString as a parameter.

    You can build this ConnectionString using an EntityConnectionStringBuilder. More specifically, assuming your underlying database is SQL Server, you can use a SqlConnectionStringBuilder to build up the value for EntityConnectionStringBuilder.ProviderConnectionString.

    Here is some code that builds up the SQL Server connection string

    var scsb = new SqlConnectionStringBuilder();
    scsb.DataSource = "localhost";
    scsb.InitialCatalog = "MyDB";
    scsb.IntegratedSecurity = true;
    

    And here’s some code that builds the Entity ConnectionString:

    var builder = new EntityConnectionStringBuilder();
    builder.Metadata = "res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl";
    builder.Provider = "System.Data.SqlClient";
    builder.ProviderConnectionString = scsb.ConnectionString;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run my unit tests automatically when I save my Eclipse project.
I coded a custom Membership provider and I want to run some Unit tests
For my current project I want to be able to load some classes from
We have a python project that we want to start testing using buildbot. Its
I'm a C++ programmer thats considering using D for a personal project I want
I've a small project that I want to share with a few others on
I'm working on a project and I want to store some easily enumerated information
I am working on a project that I want to implement AJAX, and I
I'm working on a new project and i want to do something similar (somehow)
I would like to run a Django project on a server using virtualenv in

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.