ABC. nhib.pdf connects to the PostgreSQl database,

Source: Internet
Author: User

ABC. nhib.pdf connects to the PostgreSQl database,

Abp. NHibernate dynamic database connection to PostgreSQl database

 

When I first came into contact with the Abp framework, the methods encapsulated in the framework for operating various types of data are still very useful. I am still learning about it and will use it. the methods used to operate PostgreSQL data in the nhib.pdf class library are recorded. If there are any deficiencies, let the comments give pointers and throw bricks.

If you don't talk much about it, simply start:

1. vs create a project (Form, console program, or test program)

2. NuGet obtains the class library (adp. nhib.pdf)

You also need to install a driver corresponding to pgSQl

3. Create a class that inherits the AbpModule to configure the database connection information and object ing information.

Using System. reflection; using Abp. configuration. startup; using Abp. modules; using Abp. nhib.pdf; using fluentnhib.pdf. cfg. db;/*** namespace: abpPgtest * function: Configure database * Class Name: NhHibernateModel * OPERATOR: Dong Teng * Time: 17:04:27 */namespace abpPgtest {[DependsOn (typeof (AbpNHibernateModule)] public class NhHibernateModel: AbpModule {// override PreInitialize method public override void PreInitialize () {var pgStr = "Server = localhost; Port = 5432; Database = DTDB; User Id = DT; Password = DT"; var config = Configuration. modules. abpnhib.pdf (). fluentConfiguration. database (PostgreSQLConfiguration. standard. connectionString (pgStr); config. mappings (a =>. fluentMappings. addFromAssembly (Assembly. getEntryAssembly (); // base. preInitialize ();} // rewrite the Initialize method public override void Initialize () {IocManager. registerAssemblyByConvention (Assembly. getCallingAssembly (); // base. initialize ();}}}

4. Create object and object ing

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Abp. domain. entities; using Abp. nhib.pdf. entityMappings;/*** namespace: abpPgtest. testModel * function: Database Table entity and ing * Class Name: testModel * OPERATOR: Dong Teng * Time: 17:21:19 */namespace abpPgtest. testModel {public class testModelMap: EntityMap <testModel> {public testModelMap (): base ("dt_tb_test") {// Id (x => x. id ). generatedBy. increment (); // an Id Map (x => x. company); Map (x => x. name); // References <userModel> (a =>. id ). not. lazyLoad (). column ("foreign key ID"); // used when there is an associated table in the database} public class testModel: Entity <int> {public virtual int Id {get; set ;} public virtual string Name {get; set;} public virtual string Company {get; set ;}}}

5. Create a table dt_tb_test in the database.

 

6. Register and initialize the abp connection

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

7. add data to the database

// Add data var model = new testModel {Name = "Dong Teng", Company = "Dong teng technology"}; resp. Insert (model );

Open the database to view the results:

 

8. Update Data

// Update the data var m = resp. Get (1); m. Name = "Dong Teng 1"; resp. Update (m );

View results

9. query data

Query all data

var allList = resp.GetAllList();

Query by condition

10. delete data (you can delete data in multiple ways by using the id or where condition)

// Delete data. The where condition is used to Delete Expression <Func <testModel, bool> where = a => a. Id = 3; resp. Delete (where );

A Data Entry with id 3 is deleted.

11. Conclusion: ABC. NHibernate is only an encapsulation of NHIbernate. As long as the database is correctly registered and accessed, the rest is the ORM operation database, which is simple. Other relational data can be used in a similar way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.