C
C#2y ago
populus

❔ Entity Framework - SqlException

Microsoft.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: SNI_PN11, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.
)'
Microsoft.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: SNI_PN11, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.
)'
internal static async Task Initialize(WebScraperContext context)
{
context.Database.Migrate();
}
internal static async Task Initialize(WebScraperContext context)
{
context.Database.Migrate();
}
using (var db = new WebScraperContext())
{
db.Database.EnsureCreated();
db.SaveChanges();
}
using (var db = new WebScraperContext())
{
db.Database.EnsureCreated();
db.SaveChanges();
}
public class WebScraperContext : DbContext
{
public DbSet<WebPage> WebPages { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\\MSSQLLocalDB;Database=my_localnetwork;Trusted_Connection=True;");
}
}
public class WebScraperContext : DbContext
{
public DbSet<WebPage> WebPages { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\\MSSQLLocalDB;Database=my_localnetwork;Trusted_Connection=True;");
}
}
12 Replies
populus
populus2y ago
Shouldn't this database get created automatically? or whatever the issue is, can't seem to figure it out
Keswiik
Keswiik2y ago
Looks like it isn't able to connect to the database. Try the troubleshooting steps in here: https://stackoverflow.com/questions/26007913/the-specified-localdb-instance-does-not-exist Seems to be the same error.
Stack Overflow
The specified LocalDb instance does not exist
I've been using a localDB named 'Projects' for a while now. This is the same database that is created by default ASP.NET MVC 5 project. However, suddenly it stopped and I'm not able to connect to i...
populus
populus2y ago
Yes, same error but a little different. I am using Entity Framework core 7.0.1. In SQL Server Object Explorer the database does not show up so it's not getting created in the first place. But to my understanding it is supposed to get created automatically by the code I have provided, no? This is the first time I'm attempting to create a EF database aside from the time(s) we did it in school.
Keswiik
Keswiik2y ago
Does that database instance exist? MSSQLLocalDB I mean, not the my_localnetwork database
populus
populus2y ago
You would have to clarify for me what a database instance is. The line you're referring to is copied from a functional database setup on the same PC and environment. I think it means Microsoft SQL Local DB and should correspond to the packages included with the NuGet install of Entity Framework Core and the related packages.
Keswiik
Keswiik2y ago
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-ver16 I would suggest reading through this to get a better understanding of localdb. Each instance is a different database, and they can be individually managed. Not sure if it supports auto-starting an instance when connecting, but from what I can tell, you need to create the instance and configure it at a bare minimum.
SQL Server Express LocalDB - SQL Server
Become familiar with SQL Server Express LocalDB. Developers can use this lightweight Database Engine for writing and testing Transact-SQL code.
populus
populus2y ago
Thanks, I was pretty sure I remembered everything needed to get going with this database but there is just something minor I am overlooking here somewhere. The page you linked seems to be talking about a relevant but at the same time irrelevant branch of what I'm doing. I say that because I haven't read about LocalDB specifically before and I have succeeded in creating a database with Entity Framework Core and Microsoft SQL DB previously. I did start this VS project as a console app and I am wondering whether or not that means some pre-requisites aren't configured, for example 'builder.services' which I haven't fully grasped the functionality of yet. If that is the case please let me know. Am working on reading through old lecture PDF's but for now this thread is still in need of input.
Keswiik
Keswiik2y ago
Well, you could have succeeded due to an instance with the name you specified already existing and running, or settings allowed it to auto start when you attempted to connect. There are more details in the stackoverflow post I linked on how to check that.
populus
populus2y ago
So what you're saying is that 'MSSQLLocalDB' is not a built-in variable but a customizable one to what I set it? The thing is though that I am seeing the databases from my other project(s) in the Server Object Explorer. I don't know any other lines or places where I have written 'MSSQLLocalDB' so that's why I assumed it was like saying "Use this protocol". After deleting the 'Migrations' folder and running ' dotnet ef migrations add InitialCreate ' followed up with 'dotnet ef database update' and re-building the project I notice that I don't get the configuration log in the console, suggesting that the database update isn't happening at even that stage. Even though I get a new 'Migrations' folder.
Keswiik
Keswiik2y ago
Yes, it is a variable, and you can change it to whatever you want assuming there is an instance created. But first I would use the SQLLocalDB utility and verify the instance is running and that you have the correct name.
D.Mentia
D.Mentia2y ago
Btw, by default MSSQL express listens on localhost\SQLEXPRESS. Don't know what that localdb thing is but that \SQLEXPRESS has gotten me more than once
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.