Why is the SPA application not creating a database?

519 views Asked by At

I have followed a microsoft video trying to make the single page application and it doesnt create or connect to my sql instance even though I have other applications using code first. For example the connection string was...

<add name="DefaultConnection" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />

But I get an error saying

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)

Its really strange because my other application with the connectionstring...

<add name="PteDotNetContext" providerName="System.Data.SqlClient" connectionString="Server=Localhost;Database=PteDotNet;Trusted_Connection=true;" />

Works fine and will connect no problem...

1

There are 1 answers

0
Eranga On BEST ANSWER

If you do not provide a connection string or connection string name to the DbContext constructor, it will try to find a connection name that matches the (inherited) DbContext class name.

Hence you can either change the name of the connection string

<add name="MyDbContext" connectionString="Data Source=Localhost;Initial Catalog=aspnet-MvcApplication2-201265113324;Integrated Security=True" providerName="System.Data.SqlClient" />

Or provide the name of the connection string in the constructor

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