0

I am writing Integration tests for a WPF application and I'm using a local database that is placed inside the solution for integration testing and this database has data inside it.

When I read data from this database after connecting it to SQL Server, it returns valid data, but once I go back to the application, add its connection string and try to get data, the code returns an empty set. I'm on a tight deadline and I don't know why this is happening.

Any help will be greatly appreciated.

Simple code is:

var xsystem = context.Species.ToList();

My connection string is:

<add name="MaxDatabase" 
     connectionString="Data Source=(LocalDb)\ProjectsV13;Initial Catalog=Catalog=MaxLocalEmbeded;Integrated Security=SSPI" 
     providerName="System.Data.SqlClient" />

1 Answer 1

3

Just replace your connection string with this below code. Also remove "catalog=catalog" from your connection.

<add name="MaxDatabase" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=MaxLocalEmbeded;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MaxLocalEmbeded.mdf" providerName="System.Data.SqlClient" />    
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this and it worked but what do you think is the logic behind it? what If I want the connection string to be generic so that if the local db is pushed to source control no one has to change the connection string again and again?
change your connection string Data Source To server and server value should be "." like below connection string connectionString="Server=.;Initial Catalog=MaxLocalEmbeded;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MaxLocalEmbeded.mdf"
Yes I understand that, but I'm looking for the logic behind it, why do we have to attach a database file with its exact path in the directory? can't we make it generic?
its fully dynamic i think.
how will this |DataDirectory| will be replaced then? if it is dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.