Abp.NHibernate動態(tài)庫連接PostgreSQl數(shù)據(jù)庫,供大家參考,具體內容如下
初次接觸Abp框架,其框架中封裝的操作各類數(shù)據(jù)的方法還是很好用的,本人還在進一步的學習當中,并將利用abp.NHibernate類庫操作PostgreSQL數(shù)據(jù)的相關方法做一記錄,不足之處讓評論指點扔磚。
話不多說,直接開干:
1、vs 新建一個項目,(窗體或者控制臺程序或者測試程序)
2、NuGet 獲取類庫(adp.NHibernate)
![](/d/20211018/702b35411eda4d7f62fed96d9223fd9c.gif)
還需安裝一個pgSQl 對應的驅動
![](/d/20211018/f406ca51ffbd00b231fad2fb972ea324.gif)
3、新建一個繼承AbpModule的類,用于配置數(shù)據(jù)庫連接信息和實體映射的相關信息
using System.Reflection;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.NHibernate;
using FluentNHibernate.Cfg.Db;
/**
* 命名空間: abpPgtest
* 功 能: 配置數(shù)據(jù)庫
* 類 名: NhHibernateModel
* 作 者: 東騰
* 時 間: 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、新建實體和實體映射
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ù)庫表實體及映射
* 類 名: testModel
* 作 者: 東騰
* 時 間: 2018/1/29 17:21:19
*/
namespace abpPgtest.testModel
{
public class testModelMap : EntityMaptestModel>
{
public testModelMap():base("dt_tb_test")
{
//Id(x => x.Id).GeneratedBy.Increment();//數(shù)據(jù)庫表中沒有自增的Id時需要映射一個Id
Map(x => x.Company);
Map(x => x.Name);
//ReferencesuserModel>(a => a.Id).Not.LazyLoad().Column("外鍵ID");//數(shù)據(jù)庫中有關聯(lián)表時使用
}
}
public class testModel:Entityint>
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Company { get; set; }
}
}
5、數(shù)據(jù)庫中新建表 dt_tb_test
![](/d/20211018/6c995a06cab3c471e993e50a3181e597.gif)
6、注冊并初始化abp連接
var bootstrapper = AbpBootstrapper.CreateNhHibernateModel>();
bootstrapper.Initialize();
var resp = bootstrapper.IocManager.ResolveIRepositorytestModel>>();
7、向數(shù)據(jù)庫中添加數(shù)據(jù)
//添加數(shù)據(jù)
var model = new testModel
{
Name = "東騰",
Company = "東騰科技"
};
resp.Insert(model);
打開數(shù)據(jù)庫查看結果:
![](/d/20211018/a41975e61faefcdbb336e7e3868be22c.gif)
8、更新數(shù)據(jù)
//更新數(shù)據(jù)
var m = resp.Get(1);
m.Name = "東騰1";
resp.Update(m);
查看結果
![](/d/20211018/bb838564151add2f32a4bacc144d1833.gif)
9、查詢數(shù)據(jù)
查詢所有的數(shù)據(jù)
var allList = resp.GetAllList();
![](/d/20211018/3a4a27788644b1d2a54413162da19114.gif)
按照條件進行查詢
![](/d/20211018/3478a03c612aeba014ee317eec2aed0c.gif)
10、刪除數(shù)據(jù)(可以根據(jù)多種方式進行刪除,用id或者where條件進行刪除)
//刪除數(shù)據(jù),更具where條件刪除
ExpressionFunctestModel, bool>> where = a =>a.Id==3;
resp.Delete(where);
id為3的一條數(shù)據(jù)被刪除
![](/d/20211018/3c6967e8dd01993df0785320894e5d27.gif)
11、總結:
abp.NHibernate只是ABP中對NHIbernate的一個封裝,只要正確注冊和訪問數(shù)據(jù)庫,其余的就是ORM操作數(shù)據(jù)庫,就簡單了。其他的關系型數(shù)據(jù)都用類似的做法即可。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Python實現(xiàn)連接postgresql數(shù)據(jù)庫的方法分析
- Java連接postgresql數(shù)據(jù)庫的示例代碼
- Node.js連接postgreSQL并進行數(shù)據(jù)操作
- Python連接PostgreSQL數(shù)據(jù)庫的方法
- php連接與操作PostgreSQL數(shù)據(jù)庫的方法
- PostgreSQL數(shù)據(jù)庫服務端監(jiān)聽設置及客戶端連接方法教程