【问题标题】:How to connect postgreSQL DB with ASP .NET MVC project如何将 postgreSQL DB 与 ASP .NET MVC 项目连接起来
【发布时间】:2018-08-09 21:46:30
【问题描述】:

我的.NET ASP 应用程序中的数据库连接出现问题。 App.configWeb.config 可能有问题。

它默认连接到MSSQL,但我尝试通过npgsql nuget 包和实体框架将它连接到PostgreSQL

当我想在Visual Studio 的项目中创建迁移时,它甚至看不到Database Server for PostgreSQL(Enable-Migrations; Add-Migration Initial; Update-database)。

这里是我的代码的 github 链接:

webconfig

App.config

我想知道如何使它正确并使其正常工作。

【问题讨论】:

  • 您是否进行了必要的配置以使其正常工作?我在您的代码中没有看到 npsql 文档中描述的任何方法:npgsql.org/ef6/index.html
  • 那是缺失的部分之一,但当我开始从您的链接中搜索内容时,我还没有做一些我发现的事情。谢谢:)。

标签: c# asp.net-mvc postgresql database-connection npgsql


【解决方案1】:
      using System;
   using Npgsql;         // Npgsql .NET Data Provider for PostgreSQL

   class Sample
   {
     static void Main(string[] args)
     {
        // Specify connection options and open an connection
        NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;" + 
                                "Password=pwd;Database=postgres;");
        conn.Open();

        // Define a query
        NpgsqlCommand cmd = new NpgsqlCommand("select city from cities", conn);

        // Execute a query
        NpgsqlDataReader dr = cmd.ExecuteReader();

        // Read all rows and output the first column in each row
        while (dr.Read())
          Console.Write("{0}\n", dr[0]);

       // Close connection
       conn.Close();
     }
   }

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2021-09-03
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多