在C# Entity Framework中使用SQL连接字符串中的端口:关键字不受支持:'port'。

4

我一直在使用VS code设置Entity Framework,参考了这个教程:
https://www.learnentityframeworkcore.com/walkthroughs/aspnetcore-application
然而,在执行迁移时,我遇到了这个具体的错误:Keyword not supported: 'port'。
我找到的最好的参考资料是这个stackoverflow,但由于我迄今为止一直在使用的设置,其中没有一个解决方法似乎有效。
C# Entity Framework: Keyword not supported: 'port'

这段代码似乎给我带来麻烦:

using Microsoft.EntityFrameworkCore;
namespace EFPrac
{
    
    public class EFCoreWebDemoContext : DbContext
    {
        public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {            
            string connectionString = "server=localhost;port=3306;database=EFpractice;uid=root,Pwd=****";
            optionsBuilder.UseSqlServer(connectionString);
        }
    }
}

1个回答

6

connectionstrings.com是一个很好的关于连接字符串问题的网站。

使用非标准端口

如果你的SQL Server监听在非默认端口上,你可以使用servername,xxxx语法来指定(注意逗号,不是冒号)。

Server=myServerName,myPortNumber;Database=myDataBase;User Id=myUsername;Password=myPassword;

另外,也可以尝试以下方法:

string connectionString="server=localhost,3306;database=EFpractice;uid=root,Pwd=****"

那实际上就是我用来创建连接字符串的网站!我尝试了你的修复方法,但出现了类似的错误:这次抛出了“内部连接致命错误”。 - GCK
尝试了非默认端口的解决方案,与服务器成功建立了连接,感谢您的帮助!现在我至少有一个立足点来开始调试新的错误了! - GCK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接