简体   繁体   中英

Service-Based Database Entity Framework Connection Error On released version

I am writing a Windows Forms application, where I want to save data inside the application. For the reason I used a serviced-based database with a .mdf database file, and I use Entity Framework.

So far everything is working fine until I released the file and opened it on another computer. The application opened and everything worked fine but when it comes to the database interaction, it throws a big error like this:

************ Exception Text ************
System.Data.Entity.Core.EntityException: The underlying provider failed on Open.

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.)

Here a screenshot of the error: https://www.dropbox.com/s/gwban6ab97c6fya/22093543_10213995193571435_760919436_n.png?dl=0

In case you need the project its uploaded to here: https://www.dropbox.com/s/ubpc683ggtihh6k/usercontrol.zip?dl=0

I have tried in so many ways but nothing is working, same thing happens for the installer version as well.

Can anyone help me on this please?

Here are my connection strings:

<add name="DatabaseEntities" 
     connectionString="metadata=res://*/dbModel.csdl|res://*/dbModel.ssdl|res://*/dbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"  
     providerName="System.Data.EntityClient" />
<add name="OPLCheque.Properties.Settings.DatabaseConnectionString" 
     connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True" 
     providerName="System.Data.SqlClient" />

I'm pretty sure this post would be flagged as duplicate since a similar question has been answered multiple times already, but here you go.

  • You are using LocalDB which means it cannot be shared since it runs on SQL Server Express.

If you simply want a LocalDB, see this answer to implement it after publishing your WinForms app.

Basically you want to publish your app with prerequisite components so you should publish those along your application.

  • If you want to use a single database to be shared between instances of your application, learn how to setup one using SQL Server here .

Afterwards, learn what connection string format you should use.

Sample to connect via IP Address:

connetionString="Data Source=IP_ADDRESS,PORT;
Network Library=DBMSSOCN;Initial Catalog=DatabaseName;
User ID=UserName;Password=Password"

See this for more.

  • If ever you plan on using a shared database, better make sure that the connection string that your database context should use is specified.

Config:

<add name="DefaultConnection" connectionString="" providerName="" />

Database Context:

public DbContext() 
    : base ("DefaultConnection") 
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM