如何将postgreSQL DB与ASP .NET MVC项目连接起来

问题描述 投票:0回答:1

我在.NET ASP应用程序中连接到数据库时遇到问题。也许App.configWeb.config有问题。

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

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

这是我的代码的github链接:

webconfig

App.config

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

c# asp.net-mvc postgresql database-connection npgsql
1个回答
0
投票
      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();
     }
   }
© www.soinside.com 2019 - 2024. All rights reserved.