连接本地SQL Server时出现了与EF Core 7的问题。

huangapple go评论142阅读模式
英文:

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 = &quot;Data Source = (local); Initial Catalog = SamuraiApp; Integrated Security = True;&quot;; 
                                                                                                                          //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&lt;WeatherForecast&gt; Get()
        {

            WebDbContext dbContext = new WebDbContext();
            var res = dbContext.Database.ExecuteSqlInterpolated($&quot;UPDATE battles SET Name = Name&quot;);

            Console.WriteLine(&quot;Number of rows: &quot; + res);

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index =&gt; 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: &#39;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.)&#39;

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】

  1. 在vs2022(或其他版本)中打开SQL Server对象资源管理器

    连接本地SQL Server时出现了与EF Core 7的问题。

  2. 查找数据库(SamuraiApp)

    连接本地SQL Server时出现了与EF Core 7的问题。

  3. 查找连接字符串。

    连接本地SQL Server时出现了与EF Core 7的问题。

英文:

<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>

  1. Open the sql server object explorer in the vs2022 (or other version)

    连接本地SQL Server时出现了与EF Core 7的问题。

  2. Find the DB (SamuraiApp)

    连接本地SQL Server时出现了与EF Core 7的问题。

  3. Find the connection string.

    连接本地SQL Server时出现了与EF Core 7的问题。

huangapple
  • 本文由 发表于 2023年3月8日 15:34:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定