0

I've been trying to build a code first desktop app - it works fine on my dev machine, but, I seem to be having so many errors trying to deploy it.

The main one at the moment that I can't work out is that when running, I get the following error:

Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.

Expansion of |DataDirectory| failed while processing the connection string. Ensure that |DataDirectory| is set to a valid fully-qualified path.

I just about understand Connection strings on ASP.Net/MVC/EF, but, I have been having problems learning/using them on winform projects. After a lot of searching, I found the following code on MSDN which allows the app to work on my dev machine:

public MyConfiguration()
{
    SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
    SetDefaultConnectionFactory(new LocalDbConnectionFactory("MSSQLLocalDB"));
}

I put the following code in:

System.Windows.Forms.MessageBox.Show(db.Database.Connection.ConnectionString.ToString());

Which returns this:

Data Source=|DataDirectory|Myapp.Context.sdf; 

I've tried Googling without any luck and can't seem to find anyone else on Stackoverflow with a similar issue.

I then tried a few things including removing an reinstalling the entity Framework nuget package, and now the same code returns this:

Data Source=(localdb)\mssqllocaldb;Initial Catalog=myapp.Context;Integrated Security=True;MultipleActiveResultSets=True

There is no change in app.config and I am just at a loss as to what has happened.

Can anyone understand/explain what has happened here?

7
  • Can you post the relevant snippet of code and your connection string please? Commented May 31, 2015 at 12:42
  • Can you post the connection string itself, not the factory logic? It seems you are using LocalDb - how are you deploying this WinForms app? What is the use of the SqlAzureExecutionStrategy class? You will need to create a Sql Azure database and deploy your local db to that. See stackoverflow.com/questions/27376520/… Commented May 31, 2015 at 13:23
  • @adaam - there is no connection string. I tried a few and it wasn't being used, so after reading MSDN ( msdn.microsoft.com/en-us/data/jj680699 ) I did the above. On my dev machine, this just works... on the client machine it doesn't. this is using code first, so, I would expect it to be able to create the database. Commented May 31, 2015 at 13:27
  • Connection string is in app.config for windows forms. Commented May 31, 2015 at 15:40
  • @SteveGreene - there isn't one, and when I tried to add one, it was ignored. This works on both my Dev machines, but it doesn't work when I deploy to the client... I really need help that can tell me what I am doing wrong as I have tried various things without any luck. Commented May 31, 2015 at 17:12

0

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.