user466663
user466663

Reputation: 845

Entity Framework doesn't recognize the connection

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.

Upvotes: 0

Views: 187

Answers (1)

user466663
user466663

Reputation: 845

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?

Upvotes: 1

Related Questions