EF Codefirst Winforms app - Connection string prob

2019-09-18 16:33发布

问题:

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?