0

I created a model from DbContext and stored connection string in the app config file. It runs perfectly on my PC. However when I deploy it to another PC, it throws an exception

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server )

public  class FoodIndustryContext:DbContext
    {
        public FoodIndustryContext (): base("name = FoodIndustryContextDB") { }
        public DbSet<Industry> Industries { get; set; }
        public DbSet<Purchase> Purchases { get; set; }
    }


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name = "FoodIndustryContextDB"
       connectionString = "Data Source =.;Initial Catalog = FoodIndustryDemo;Integrated Security = true"
       providerName = "System.Data.SqlClient"/>
  </connectionStrings>
</configuration>
Kinyo356
  • 135
  • 13
  • 1
    `Data Source=.` means the local computer. I assume where you deployed your app does not also have the database. – Crowcoder Mar 09 '19 at 01:47
  • I don't understand you well @Crowcoder – Kinyo356 Mar 09 '19 at 01:51
  • 1
    It works on your PC because you have the database server ON YOUR PC. When you put the application on another computer the connection string is looking for the datbase on *itself* but there is no database installed on itself. The `Data Source` attribute usually has a server name instead of just `.`. – Crowcoder Mar 09 '19 at 01:57
  • @Kinyo356 are your database and application in different computer? – TanvirArjel Mar 09 '19 at 02:14
  • It worked perfectly as you said @Crowcoder. I changed Data Source=(LocalDB)\v11.0. Please re-write it as an answer, let me accept it as the correct answer. And also, if you have any other suggestion of what the data source should be. – Kinyo356 Mar 09 '19 at 02:31
  • No @TanvirArjel. I just wanted to see how it will work on another PC – Kinyo356 Mar 09 '19 at 02:33
  • this question indeed has nothing to do with entity framework. – Lei Yang Mar 09 '19 at 03:02
  • You can't expect Local db to be installed on a general user's computer so you may run into trouble yet still. If you intend for the database to always be local instead of a central dedicated server then you will have to handle installing it too. – Crowcoder Mar 09 '19 at 11:09

1 Answers1

0

if you take remote sql server, you have to enable firewall of the remote system.

See following link. How do I fix the error 'Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'?

Saba
  • 61
  • 9