ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驅動連接 PostgreSQL數據庫


前段時間在園子里看到了小蝶驚鴻 發布的有關綠色版的Linux.NET——“Jws.Mono”。由於我對.Net程序跑在Linux上非常感興趣,自己也看了一些有關mono的資料,但是一直沒有時間抽出時間來研究這個,小蝶驚鴻的博客又激起了我的興趣,我花了四天的時間,終於在Liunx上跑起了屬於我自己的應用程序,其中數據庫使用到了PostgreSQL數據庫。對於數據庫的選用,是在小蝶驚鴻 博客Linux.NET學習手記(4)中,使用了這個數據庫。

今天,我只是單純講解使用ASP.NET MVC  + 微型orm框架 Petapoco  連接PostgreSQL數據庫。C#操作PostgreSQL數據庫很多人應該很了解,只需要使用NpgSql驅動即可。有關NpgSql的使用大家可以參考張善友老師的博客PostgreSQL的.NET驅動程序Npgsql。關於PetaPoco的介紹和使用方法,各位讀者可以參考:PetaPoco官網.NET對象關系映射器PetaPocoOoC's BlogPetaPoco入門(二)PetaPoco入門(一)小巧方便的ORM類庫——PetaPoco(這是我在網上找了很長時間的資料啊),它們都有比較清晰而詳細的介紹PetaPoco如何使用。

由於是第一次使用PostgreSQL數據庫,我在使用的過程中遇到了許多問題,有些問題沒有截圖,我只把有截圖的一個問題給大家貼出來,然后再給大家詳解我的代碼。

image

這個問題很簡單,就是沒有找到NpgSql驅動,但是我已經把驅動程序加載到解決方案中了,為什么還會出現這個問題呢,我在google上找了很多資料,包括Petapoco 的源碼和單元測試,都沒有找到解決方案。后來在一個國外的交流網站上找到了解決方案,因為mvc應用程序需要自己手動配置webconfig文件中的驅動程序,所以我在配置文件中加了如下的配置:

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

這樣問題就輕松的解決了。看效果圖:(示例地址:http://www.cnicode.com/mvc演示

下面,我們來看一下代碼實現:

1. 在看代碼前,我們需要將NpgSql和Petapoco 加載到當前項目中來,我將使用Nuget來添加到當前項目,分別如下:

Install-Package Npgsql
Install-Package PetaPoco

2.下面看一下Web.config中的重要代碼

1>數據庫連接字符串

<connectionStrings>
    <add name ="Postgresql" connectionString="Server=127.0.0.1;User id=postgres;password=123;Database=mono_test;" providerName="Npgsql"/>
  </connectionStrings>

2>NpgSql驅動配置文件

<!--provider驅動的配置文件-->
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider"
           invariant="Npgsql"
           support="FF"
           description=".Net Framework Data Provider for Postgresql Server"
           type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>

3.看一下整體的項目結構

image

  4.UserInfo.cs實體類中的代碼

namespace PetaPoco
{
    [TableName("userinfo")]
    [PrimaryKey("id")]
    [ExplicitColumns]
    public class UserInfo
    {
        [Column("id")]
        public int Id { get; set; }

        [Column("name")]
        public string Name { get; set; }

        [Column("age")]
        public int Age { get; set; }

        [Column("qq")]
        public int Qq { get; set; }
    }
}

5.Controllers中的代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PetaPoco;

namespace PostgreSqlDemo.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        Database db = new PetaPoco.Database("Postgresql");
        public ActionResult Index()
        {
            ViewData.Model = db.Query<UserInfo>("select * from userinfo");
            return View();
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            ViewData.Model = db.SingleOrDefault<UserInfo>("select * from userinfo where id=@0", id);
            return View();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(UserInfo user)
        {
            try
            {
                db.Insert(user);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData.Model = db.SingleOrDefault<UserInfo>("where id=@0", id);
            return View();
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(UserInfo user)
        {
            try
            {
                db.Update(user);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id)
        {
            ViewData.Model = db.SingleOrDefault<UserInfo>("where id=@0",id);
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(UserInfo user)
        {
            try
            {
                db.Delete(user);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

6.view中的代碼,會使用asp.net mvc 就能寫出,這里就不貼出代碼了。

ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驅動連接 PostgreSQL數據庫就基本結束了,后面我會錄制一個和這篇博文對應的視頻教程,源碼和視頻教程會在稍后的博文中發布。

最后感謝張善友老師和小蝶驚鴻的博客,尤其感謝小蝶驚鴻在QQ上給我的幫助。

作者:郝喜路    2014年5月15日16:47:44

博客地址:http://www.cnblogs.com/haoxilu/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM