0

I got a demo C# solution from a third-party written using VS2019. They use the Northwind database and entity framework. It's an MVC 4 project. They have the connection string as follows:

<add name="NorthwindEntities" connectionString="metadata=res://*/Models.Northwind.csdl|res://*/Models.Northwind.ssdl|res://*/Models.Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

I have replaced the above connection string with this one.

<add name="NorthwindEntities" connectionString="metadata=res://*/Models.Northwind.csdl|res://*/Models.Northwind.ssdl|res://*/Models.Northwind.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MYSERVER_NAME\v15.0;attachdbfilename=|DataDirectory|\Northwind.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

When I ran the solution, I got the following error:

"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: 26 - Error Locating Server/Instance Specified)"

I am new to asp.net MVC and entity framework. So, I am not sure what "metadata=res:///Models.Northwind.csdl|res:///Models.Northwind.ssdl|res://*/Models.Northwind.msl;" means or why it's used. In a web forms applications, I use the following connection string, which works fine. "Server=MYSERVER_NAME;Database=NORTHWIND;Trusted_Connection=True;Encrypt=false"

public ActionResult 
Products_Read([DataSourceRequest]DataSourceRequest request)
    {
        using (var northwind = new NorthwindEntities())
        {
            IQueryable<Product> products = northwind.Products;
            // Convert the Product entities to ProductViewModel 
            //instances
            DataSourceResult result = 
             products.ToDataSourceResult(request, product => new 
             ProductViewModel
            {
                ProductID = product.ProductID,
                ProductName = product.ProductName,
                UnitsInStock = product.UnitsInStock
            });
            return Json(result);
        }
    }

This line of the above code, "products.ToDataSourceResult(request, product => new ProductViewModel" throws the error

Their database is SQL server express and mine is a SQL server developer edition. I wonder how I could update the connection string from the third-party code.

user466663
  • 815
  • 4
  • 18
  • 40
  • 1
    You checked connection to `Server=MYSERVER_NAME;Database=NORTHWIND` but the new connectionstring contains `data source=MYSERVER_NAME\v15.0`. Did you check the wrong connection or did you set the wrong connectionstring? Also: SQL Server Express LocalDB doesn't accept remote connections. If you try to connect to a localdb on remote computer, it will not work. – Sebastian Siemens Jan 26 '23 at 21:36
  • I have replaced, "data source=MYSERVER_NAME;" with "Server=(LocalDB);" and still getting the same error – user466663 Jan 26 '23 at 21:48

1 Answers1

1

Finally, I have resolved the issue. So, the only change was “(LocalDB)\v11.0” to “(LocalDB)\MSSQLLocalDB”. This may be because v11.0 works only in SQL server 2014. Since I had SQL server 2016 and later, I had to change it to the one shown above. Link: How to install localdb separately?

user466663
  • 815
  • 4
  • 18
  • 40