英文:
Problem with connecting on local SQL Server with EF Core 7
问题
以下是您要的内容的翻译:
I have a simple web application, it is generated from Visual Studio on version .NET 5 and EF Core 5, and the only thing added is WebDbContext and SQL query to see if the database connection works.
WebDbContext:
namespace WebApplication1.Data
{
public class WebDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "Data Source=(local); Initial Catalog=SamuraiApp; Integrated Security=True;";
optionsBuilder.UseSqlServer(connectionString)
.LogTo(Console.WriteLine,
new[] {
DbLoggerCategory.Database.Command.Name,
}
, LogLevel.Information
)
.EnableSensitiveDataLogging();
}
}
}
Get API:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
WebDbContext dbContext = new WebDbContext();
var res = dbContext.Database.ExecuteSqlInterpolated($"UPDATE battles SET Name = Name");
Console.WriteLine("Number of rows: " + res);
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
This app works and it connects to the database when you are using .NET 5 and EF Core 5. If you are using .NET 6 and EF Core 6, it still works. If you are using .NET 7 and EF Core 6, it works, but if you use .NET 7 and EF Core 7, it cannot connect to the database.
It shows an error:
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: TCP Provider, error: 0 - No such host is known.)'
I haven't changed anything in the code when I changed versions. I have a database in this location, and it works on the older version of EF Core, but it doesn't work with EF Core 7. Is something changed in EF Core 7 about connecting to a server, which is on a local machine and has an alias "local"?
英文:
I have simple web application, it is generated from Visual Studio on version .NET 5 and EF Core 5, and only thing added is WebDbContext and sql query to see if database connection works.
WebDbContext:
namespace WebApplication1.Data
{
public class WebDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "Data Source = (local); Initial Catalog = SamuraiApp; Integrated Security = True;";
//Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MyWorldDB;Integrated Security=True;Connect Timeout=30
optionsBuilder.UseSqlServer(connectionString)
.LogTo(Console.WriteLine,
new[] {
DbLoggerCategory.Database.Command.Name,
}
, LogLevel.Information
)
.EnableSensitiveDataLogging();
}
}
}
Get API:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
WebDbContext dbContext = new WebDbContext();
var res = dbContext.Database.ExecuteSqlInterpolated($"UPDATE battles SET Name = Name");
Console.WriteLine("Number of rows: " + res);
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
This app works and it connects on database when you are using .net 5 and EF Core 5, if you are using .net 6 and EF Core 6 it still works, if you are using .net 7 and EF Core 6 it works but if you use .net 7 and EF Core 7 it cannot connect to database.
It shows error:
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: TCP Provider, error: 0 - No such host is known.)'
I haven't change something in code when I changed versions. I have database on this location and it works on older version of EF Core, but it doesn't work EF Core 7. Is something changed in EF Core 7 about connecting to server, which is on local machine and it has allias local?
答案1
得分: 1
【H3】更新【/H3】
我仔细检查了错误信息,还有另一个可能导致问题的原因。请检查链接以进行配置。我们不需要ApexSQL工具,只需按照文档配置SQL Server。
如何使用ApexSQL工具配置远程访问并连接到远程SQL Server实例
【H3】之前【/H3】
根据错误消息,您可以按照我的步骤获取正确的连接字符串。
正确的格式应如下:
Server=(localdb)\\mssqllocaldb;Database=aspnet-Core6MVCSignalR-64406da8-b697-43cb-9c46-aaa950b705b1;Trusted_Connection=True;MultipleActiveResultSets=true
【h3】步骤【/h3】
英文:
<H3>UPDATE</H3>
I double check the error message, there are another reason may cause the issue. Pls check the link to configure it. We don't need ApexSQL tools, just follow the doc to configure sql server.
How to configure remote access and connect to a remote SQL Server instance with ApexSQL tools
<H3>PREVIOUS</H3>
According the error message, you could follow my steps to get the correct connection string.
The correct format should be like below:
Server=(localdb)\\mssqllocaldb;Database=aspnet-Core6MVCSignalR-64406da8-b697-43cb-9c46-aaa950b705b1;Trusted_Connection=True;MultipleActiveResultSets=true
<h3>Steps</h3>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论