Follow us

PetaPoco - DBEntityGenerator - Easy and fast way to perform Database related operations in .net

 

Hi Viewers,

 

This article continue to my last article "PetaPoco - Easy and fast way to perform Database related operations in .net" and in this article we will learn how to generate your database tables as poco model. So that instead of creating all the respective models manually, you can simply use PetaoPoco generator to generate all the database tables as Model in C# and start using it.

 

Please follow below steps -

 

  1. Install "PetaPoco.DBEntityGenerator" from nuget to your project.
  2. Create folder "Model" in your project just to keep your generated tabase tables as model class and PetaPoco config.json file inside.
  3. Create .json file (database_config.json) and put all the configuration for your database. This configuration data will be used when you will run command to generate model then based on configuration setting db model will be generated to your project.

           "database_config.json" - Sample code I used

           

 

  1. Fyi - when you install "PetaPoco.DBEntityGenerator" in above step 1 then it installed .net core and .net framework both generator exe which you can use to generate the model and by default it installed at location (C:\Users\<user>\.nuget\packages\petapoco.dbentitygenerator\0.1.0\tools\net45). In this example I am using .net framework. So go to installed location, open CMD and run below command

 

PetaPoco.DBEntityGenerator.exe --config "C:\TestConsolePOC.PetaPoco\Model\database_config.json" --output "file" --outputFile "C:\TestConsolePOC.PetaPoco\Model\\Database.cs"

 

Fyi - here "PetaPoco.DBEntityGenerator.exe" is the installed exe, "--config" parameter is taking path of the json configuration file defined above and "--output" parameter is specifying output as file and "--outputFile" parameter is taking path where all the database tables as model will be generated.

 

So post successful run, it will generate the model(refer below) and you can start using it.

 

And that's it. Now you start using your models from this Databse.cs file instead of having manual file/class "Member" created in previous example.

 

 

 

Categories/Tags: petapoco~peta poco~dbentitygenerator~db entity generator~PetaPoco.DBEntityGenerator

Recent Articles

1

AWS Saving Plan - Cost optimization tips

2
3

AWS RDS Key Concepts & Why you should use it?

4
5

Open-Search/Kibana - Multi Tenancy Setup

See All Articles