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/

posted @ 2014-05-15 16:50  郝喜路  阅读(5827)  评论(9编辑  收藏  举报
友情链接:运动人生