Abp.nhibernate Dynamic Library connection to PostgreSQL database
The initial contact with the ABP framework, the framework of the operation of various types of data in the method is still very useful, I am still in the further study, and will use ABP. NHibernate Class Library operation PostgreSQL data related methods to do a record, the shortcomings of the comments to throw bricks.
Talk not much, directly open dry:
1. VS Create a new project (form or console program or test program)
2. NuGet Gets the class library (ADP. NHibernate)
Also need to install a pgsql corresponding driver
3. Create a new class that inherits Abpmodule to configure information about database connection information and entity mappings
usingSystem.Reflection;usingAbp.Configuration.Startup;usingAbp.modules;usingabp.nhibernate;usingFluentNHibernate.Cfg.Db;/** * Namespace: abppgtest* Function: Metabase * class Name: nhhibernatemodel* Author: Dong Teng * Time: 2018/1/29 17:04:27*/namespaceabppgtest{[DependsOn (typeof(Abpnhibernatemodule))] Public classNhhibernatemodel:abpmodule {//overriding the Preinitialize method Public Override voidpreinitialize () {varPgstr ="Server=localhost; Port=5432;database=dtdb; User Id=dt; Password=dt"; varConfig =Configuration.Modules.AbpNHibernate (). Fluentconfiguration. Database (PostgreSQLConfiguration.Standard.ConnectionString (PGSTR)); Config. Mappings (A=a.fluentmappings.addfromassembly (assembly.getentryassembly ())); //base. Preinitialize (); } //overriding the Initialize method Public Override voidInitialize () {iocmanager.registerassemblybyconvention (assembly.getcallingassembly ()); //base. Initialize (); } }}
4. New Entity and entity mappings
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingAbp.Domain.Entities;usingAbp.NHibernate.EntityMappings;/** * Namespace: abppgtest.testmodel* function: Database table entity and map * Class name: testmodel* Author: Dong Teng * Time: 2018/1/29 17:21:19*/namespaceabppgtest.testmodel{ Public classTestmodelmap:entitymap<testmodel> { PublicTestmodelmap ():Base("dt_tb_test") { //Id (x = x.id). Generatedby.increment ();//You need to map an ID when there is no self-growing ID in the database tableMap (x =X.company); Map (x=x.name); //references<usermodel> (a = a.id). Not.lazyload (). Column ("Foreign Key ID");//used when there is an associated table in the database } } Public classtestmodel:entity<int> { Public Virtual intId {Get;Set; } Public Virtual stringName {Get;Set; } Public Virtual stringCompany {Get;Set; } }}
5. New Table Dt_tb_test in database
6. Register and initialize the ABP connection
var bootstrapper = abpbootstrapper.create<nhhibernatemodel>(); bootstrapper. Initialize (); var resp = bootstrapper. Iocmanager.resolve<irepository<testmodel>> ();
7. Adding data to the database
// Add Data var New Testmodel { " Dong Teng ", " Dong Teng technology " }; Resp. Insert (model);
Open the database to view the results:
8. Update data
// Update Data var m = resp. Get (1); " Dong Teng 1 " ; Resp. Update (m);
View Results
9. Query data
Querying all the data
var alllist = resp. Getalllist ();
Query by criteria
10, delete data (can be deleted according to a variety of ways, with ID or where condition to delete)
// delete data, more where condition delete BOOL where = a =>a.id==3; Resp. Delete (where);
A piece of data with ID 3 is deleted
11, Summary: ABP. NHibernate is only an ABP in the NHibernate package, as long as the correct registration and access to the database, the rest is the ORM operation database, it is simple. Other relational data are done in a similar way.
Abp.nhibernate Connecting the PostgreSQL database