天天看點

EntityFramework6(EF6)連接配接Postgresql資料庫

軟體:VS2017 Community、Postgresql

1.建立一個MVC架構的ASP.NET Web應用程式項目:

EntityFramework6(EF6)連接配接Postgresql資料庫

2.打開菜單欄工具下的擴充與更新,找到Npgsql PostgreSQL Interaction插件并安裝,這樣才能在server explorer中連接配接到postgresql資料庫:

EntityFramework6(EF6)連接配接Postgresql資料庫

3.使用Nuget程式包管理器控制台安裝連接配接所需要的Nuget包,輸入如下代碼:

install-package npgsql -version 3.2.7
install-Package EntityFramework6.Npgsql -Version 3.1.1
           

安裝完後即可在解決方案管理器的引用下看到如下所添加的引用:

EntityFramework6(EF6)連接配接Postgresql資料庫

4.做完這些準備後,接下來開始連接配接postgresql,打開菜單欄工具下的“連接配接到資料庫”,選擇postgresql,輸入所要連接配接的資料庫名,完成資料庫連接配接:

EntityFramework6(EF6)連接配接Postgresql資料庫
EntityFramework6(EF6)連接配接Postgresql資料庫

5.接下來建立一個實體資料模型,選擇Models檔案夾右擊添加/建立項,然後選擇資料欄裡的ADO.NET實體資料模型:

EntityFramework6(EF6)連接配接Postgresql資料庫

選擇來自資料庫的EF設計器,進行如下操作:

EntityFramework6(EF6)連接配接Postgresql資料庫
EntityFramework6(EF6)連接配接Postgresql資料庫

 注:連接配接設定另存為的檔案名同時也是後續連接配接所要建立的類名

EntityFramework6(EF6)連接配接Postgresql資料庫

模型建立完成後會在Models檔案夾下生成Model1.edmx檔案,如下:

EntityFramework6(EF6)連接配接Postgresql資料庫
EntityFramework6(EF6)連接配接Postgresql資料庫

6.建立好模型後,我們在建立一個模型的控制器,選擇Conteoller檔案夾右擊添加/控制器,選擇空的MVC 5控制器:

EntityFramework6(EF6)連接配接Postgresql資料庫

7.在Web config 裡添加如下代碼:

<system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" support="FF" />
    </DbProviderFactories>
  </system.data>
           

若沒有添加則會發生如下錯誤:

EntityFramework6(EF6)連接配接Postgresql資料庫

8.在控制器的Index方法裡編輯如下代碼(以及相應的using語句):

using Test.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Test.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        {
            Database myDatabase = new Database();
            //查詢
            var list = from s in myDatabase.student
                       select s;
            return View(list);
        }
    }
}
           

9.接着添加一個視圖來顯示我們查詢的資料,滑鼠點選View()函數右擊添加視圖:

EntityFramework6(EF6)連接配接Postgresql資料庫

10.編輯視圖,在<div>裡添加table,以及添加相應的引用,代碼如下:

@model IQueryable<Test.Models.student>
@using Test.Models
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table >
            <tr>
                <th>學号</th>
                <th>姓名</th>
            </tr>
            @foreach (student s in Model)
            {
                <tr>
                    <td>@s.sno</td>
                    <td>@s.sname</td>
                </tr>
            }
        </table>
    </div>
</body>
</html>
           

11.最後将預設的控制器改成剛剛我們建立的控制器,編輯App_Start檔案夾下的RouteConfig.cs檔案,将controlre的參數改成Test(即控制器名),運作即可檢視查詢結果。

注:若出現依賴錯誤,可能是因為Nuget包版本問題,隻需更新後即可解決

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
            );
           
EntityFramework6(EF6)連接配接Postgresql資料庫

繼續閱讀