EF Codefirst Winforms app - Connection string problems

血红的双手。 提交于 2019-12-13 05:25:31

问题


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?

来源:https://stackoverflow.com/questions/30557230/ef-codefirst-winforms-app-connection-string-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!