Abp.NHibernate連接PostgreSQl數(shù)據(jù)庫(kù)的方法
Abp.NHibernate動(dòng)態(tài)庫(kù)連接PostgreSQl數(shù)據(jù)庫(kù),供大家參考,具體內(nèi)容如下
初次接觸Abp框架,其框架中封裝的操作各類數(shù)據(jù)的方法還是很好用的,本人還在進(jìn)一步的學(xué)習(xí)當(dāng)中,并將利用abp.NHibernate類庫(kù)操作PostgreSQL數(shù)據(jù)的相關(guān)方法做一記錄,不足之處讓評(píng)論指點(diǎn)扔磚。
話不多說(shuō),直接開(kāi)干:
1、vs 新建一個(gè)項(xiàng)目,(窗體或者控制臺(tái)程序或者測(cè)試程序)
2、NuGet 獲取類庫(kù)(adp.NHibernate)

還需安裝一個(gè)pgSQl 對(duì)應(yīng)的驅(qū)動(dòng)

3、新建一個(gè)繼承AbpModule的類,用于配置數(shù)據(jù)庫(kù)連接信息和實(shí)體映射的相關(guān)信息
using System.Reflection;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.NHibernate;
using FluentNHibernate.Cfg.Db;
/**
* 命名空間: abpPgtest
* 功 能: 配置數(shù)據(jù)庫(kù)
* 類 名: NhHibernateModel
* 作 者: 東騰
* 時(shí) 間: 2018/1/29 17:04:27
*/
namespace abpPgtest
{
[DependsOn(typeof(AbpNHibernateModule))]
public class NhHibernateModel:AbpModule
{
//重寫PreInitialize方法
public override void PreInitialize()
{
var pgStr = "Server=localhost;Port=5432;Database=DTDB;User Id=DT;Password=DT";
var config = Configuration.Modules.AbpNHibernate().FluentConfiguration
.Database(PostgreSQLConfiguration.Standard.ConnectionString(pgStr));
config.Mappings(a => a.FluentMappings.AddFromAssembly(Assembly.GetEntryAssembly()));
//base.PreInitialize();
}
//重寫Initialize方法
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetCallingAssembly());
// base.Initialize();
}
}
}
4、新建實(shí)體和實(shí)體映射
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Entities;
using Abp.NHibernate.EntityMappings;
/**
* 命名空間: abpPgtest.testModel
* 功 能: 數(shù)據(jù)庫(kù)表實(shí)體及映射
* 類 名: testModel
* 作 者: 東騰
* 時(shí) 間: 2018/1/29 17:21:19
*/
namespace abpPgtest.testModel
{
public class testModelMap : EntityMap<testModel>
{
public testModelMap():base("dt_tb_test")
{
//Id(x => x.Id).GeneratedBy.Increment();//數(shù)據(jù)庫(kù)表中沒(méi)有自增的Id時(shí)需要映射一個(gè)Id
Map(x => x.Company);
Map(x => x.Name);
//References<userModel>(a => a.Id).Not.LazyLoad().Column("外鍵ID");//數(shù)據(jù)庫(kù)中有關(guān)聯(lián)表時(shí)使用
}
}
public class testModel:Entity<int>
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Company { get; set; }
}
}
5、數(shù)據(jù)庫(kù)中新建表 dt_tb_test

6、注冊(cè)并初始化abp連接
var bootstrapper = AbpBootstrapper.Create<NhHibernateModel>(); bootstrapper.Initialize(); var resp = bootstrapper.IocManager.Resolve<IRepository<testModel>>();
7、向數(shù)據(jù)庫(kù)中添加數(shù)據(jù)
//添加數(shù)據(jù)
var model = new testModel
{
Name = "東騰",
Company = "東騰科技"
};
resp.Insert(model);
打開(kāi)數(shù)據(jù)庫(kù)查看結(jié)果:

8、更新數(shù)據(jù)
//更新數(shù)據(jù) var m = resp.Get(1); m.Name = "東騰1"; resp.Update(m);
查看結(jié)果

9、查詢數(shù)據(jù)
查詢所有的數(shù)據(jù)
var allList = resp.GetAllList();

按照條件進(jìn)行查詢

10、刪除數(shù)據(jù)(可以根據(jù)多種方式進(jìn)行刪除,用id或者where條件進(jìn)行刪除)
//刪除數(shù)據(jù),更具where條件刪除 Expression<Func<testModel, bool>> where = a =>a.Id==3; resp.Delete(where);
id為3的一條數(shù)據(jù)被刪除

11、總結(jié):
abp.NHibernate只是ABP中對(duì)NHIbernate的一個(gè)封裝,只要正確注冊(cè)和訪問(wèn)數(shù)據(jù)庫(kù),其余的就是ORM操作數(shù)據(jù)庫(kù),就簡(jiǎn)單了。其他的關(guān)系型數(shù)據(jù)都用類似的做法即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
postgresql 實(shí)現(xiàn)獲取所有表名,字段名,字段類型,注釋
這篇文章主要介紹了postgresql 實(shí)現(xiàn)獲取所有表名,字段名,字段類型,注釋操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
postgresql 刪除重復(fù)數(shù)據(jù)的幾種方法小結(jié)
這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)的幾種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
PostgreSQL pg_ctl start啟動(dòng)超時(shí)實(shí)例分析
這篇文章主要給大家介紹了關(guān)于PostgreSQL pg_ctl start啟動(dòng)超時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
postgresql 13.1 insert into select并行查詢的實(shí)現(xiàn)
這篇文章主要介紹了解決postgresql insert into select無(wú)法使用并行查詢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
psql除法保留小數(shù),實(shí)現(xiàn)向上取整和向下取整操作
這篇文章主要介紹了psql除法保留小數(shù),實(shí)現(xiàn)向上取整和向下取整操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Postgresql 查看SQL語(yǔ)句執(zhí)行效率的操作
這篇文章主要介紹了Postgresql 查看SQL語(yǔ)句執(zhí)行效率的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
PostgreSQL樹(shù)形結(jié)構(gòu)的遞歸查詢示例
這篇文章主要給大家介紹了關(guān)于PostgreSQL樹(shù)形結(jié)構(gòu)的遞歸查詢的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

