【问题标题】:ASP.NET Core API: How to use MySQLASP.NET Core API:如何使用 MySQL
【发布时间】:2018-09-10 08:12:15
【问题描述】:

这是我的连接字符串:

Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;

我是这样使用的:

services.AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseUrl")));

我无法连接到我的数据库,我收到了这个错误:

System.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:25 - 连接字符串无效)

System.ComponentModel.Win32Exception (0x80004005):参数不正确

在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)

于是我尝试了HeidiSQL,如下:

它成功了。

所以我必须错过UseSqlServer.. 的一些设置。

(我在关注this tutorial

【问题讨论】:

    标签: asp.net asp.net-web-api entity-framework-core asp.net-core-2.0 asp.net-core-webapi


    【解决方案1】:

    System.Data.SqlClient.SqlException 适用于 Microsoft SQL Server - 但它适用于 MySQL - 这是两个完全不同的数据库系统!

    您需要使用适当的、特定于 MySQL 的类!

    查看此页面:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html

    Entity Framework Core 有一个 MySQL 连接器,您可以通过指定来使用它:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
       .....         
       optionsBuilder.UseMySQL("Server=sql145.main-hosting.eu:3306;Database=u230450666_aspt;User=u230450666_aspt;Password=111111;");
    }
    

    【讨论】:

      【解决方案2】:

      正如@marc_s 指出的那样,您可以覆盖OnConfiguring 以使用MySQL。
      或者,如果您希望采用更简洁、更“模式友好”的方式:check this out

      【讨论】:

        猜你喜欢
        • 2017-11-01
        • 2018-12-17
        • 2017-07-10
        • 1970-01-01
        • 2019-12-05
        • 2017-07-06
        • 2018-05-15
        • 1970-01-01
        • 2022-12-14
        相关资源
        最近更新 更多