各位用户为了找寻关于Abp.NHibernate连接PostgreSQl数据库的方法的资料费劲了很多周折。这里教程网为您整理了关于Abp.NHibernate连接PostgreSQl数据库的方法的相关资料,仅供查阅,以下为您介绍关于Abp.NHibernate连接PostgreSQl数据库的方法的详细内容

Abp.NHibernate动态库连接PostgreSQl数据库,供大家参考,具体内容如下

初次接触Abp框架,其框架中封装的操作各类数据的方法还是很好用的,本人还在进一步的学习当中,并将利用abp.NHibernate类库操作PostgreSQL数据的相关方法做一记录,不足之处让评论指点扔砖。

话不多说,直接开干:

1、vs 新建一个项目,(窗体或者控制台程序或者测试程序)

2、NuGet 获取类库(adp.NHibernate)

还需安装一个pgSQl 对应的驱动

3、新建一个继承AbpModule的类,用于配置数据库连接信息和实体映射的相关信息

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 using System.Reflection; using Abp.Configuration.Startup; using Abp.Modules; using Abp.NHibernate; using FluentNHibernate.Cfg.Db;   /** * 命名空间: abpPgtest * 功 能: 配置数据库 * 类 名: 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、新建实体和实体映射

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 * 功 能: 数据库表实体及映射 * 类 名: testModel * 作 者: 东腾 * 时 间: 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();//数据库表中没有自增的Id时需要映射一个Id       Map(x => x.Company);       Map(x => x.Name);         //References<userModel>(a => a.Id).Not.LazyLoad().Column("外键ID");//数据库中有关联表时使用       }   }   public class testModel:Entity<int>   {     public virtual int Id { get; set; }       public virtual string Name { get; set; }       public virtual string Company { get; set; }   } }

5、数据库中新建表 dt_tb_test

6、注册并初始化abp连接

? 1 2 3 var bootstrapper = AbpBootstrapper.Create<NhHibernateModel>(); bootstrapper.Initialize(); var resp = bootstrapper.IocManager.Resolve<IRepository<testModel>>();

7、向数据库中添加数据

? 1 2 3 4 5 6 7 //添加数据   var model = new testModel     {      Name = "东腾",      Company = "东腾科技"     };  resp.Insert(model);

打开数据库查看结果:

8、更新数据

? 1 2 3 4 //更新数据   var m = resp.Get(1);   m.Name = "东腾1";   resp.Update(m);

查看结果

9、查询数据

查询所有的数据

? 1 var allList = resp.GetAllList();

按照条件进行查询

10、删除数据(可以根据多种方式进行删除,用id或者where条件进行删除)

? 1 2 3 //删除数据,更具where条件删除   Expression<Func<testModel, bool>> where = a =>a.Id==3;   resp.Delete(where);

id为3的一条数据被删除

11、总结:

abp.NHibernate只是ABP中对NHIbernate的一个封装,只要正确注册和访问数据库,其余的就是ORM操作数据库,就简单了。其他的关系型数据都用类似的做法即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://www.cnblogs.com/dongteng/archive/2018/01/30/8378650.html