Search code examples
c#.netormpetapoco

How to automatically create model from database using PetaPoco library?


I have a table in my database for which I want to create a model class with getters and setters. For most of the tasks in my project, I am using PetaPoco. I created models manually, but few tables have lots of columns.

Is there any way to create a model from a database using PetaPoco?


Solution

  • PetaPoco has T4 Visual Studio templates that should do this for you. If you don't want to have them all generated, just use the template and then copy those classes from the resulting *.cs file that you want.

    You'll find templates on PetaPoco's GitHub code repo.

    Adding templates to your project can be done in two ways:

    1. copying files manually
    2. using NuGet in the package console or using Visual Studio Package Manager. Details on NuGet site

    I would recommend the second option, because you can set your packages to auto download before build when not present (part of your CVS).