How to automatically create a model from a database using the PetaPoco library?

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

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

+3
source share
3 answers

PetaPoco has Visual Studio T4 templates that should do this for you. If you do not want all of them to be generated, just use the template, and then copy these classes from the resulting * .cs file that you want.

You will find the templates in the PetaPoco GitHub Code Repository .

:

  • NuGet Visual Studio. NuGet

, , ( CVS).

+4

T4, , .

- " " ( "" > " " ), " " ), , " ", :

PM>install-package petapoco

"" "Generated". "" "Database.tt" . http://www.toptensoftware.com/petapoco/.

T4, "Database.cs" . " ". , . , . / , .

- TSQL . , , PetaPoco.

declare @script nvarchar(max);
declare @table nvarchar(256);

set @table = 'YourTableName'

set @script = 'public interface I' + @table + '{' + char(10);
SELECT
    @script = @script + 
        CASE
            WHEN st.Name IN ('int') AND c.is_nullable = 0 THEN 'int'
            WHEN st.name in ('smallint')  AND c.is_nullable = 0 THEN 'short'
            WHEN st.name IN ('bigint') AND c.is_nullable = 0  THEN 'long'
            WHEN st.name IN ('varchar','nvarchar','sysname') THEN 'string'
            WHEN st.Name IN ('datetime') AND c.is_nullable = 0 THEN 'DateTime'
            WHEN st.Name IN ('bit') AND c.is_nullable = 0 THEN 'bool'
            WHEN st.Name IN ('decimal') AND c.is_nullable = 0 THEN 'decimal'
            /* NULLABLE VALUES */
            WHEN st.Name IN ('int') AND c.is_nullable = 1 THEN 'int?'
            WHEN st.name in ('smallint')  AND c.is_nullable = 1 THEN 'short?'
            WHEN st.name IN ('bigint') AND c.is_nullable = 1  THEN 'long?'
            WHEN st.name IN ('varchar','nvarchar','sysname') AND c.is_nullable = 1 THEN 'string?'
            WHEN st.Name IN ('datetime') AND c.is_nullable = 1 THEN 'DateTime?'
            WHEN st.Name IN ('bit') AND c.is_nullable = 1 THEN 'bool?'
            WHEN st.Name IN ('decimal') AND c.is_nullable = 1 THEN 'decimal?'   
            --WHEN st.name IN('sysname') AND c.is_nullable = 1 THEN 'string?'       
            ELSE 'UNKOWN-' + st.name
        END
    + ' ' + c.name + '{get;set;}' + char(10)
FROM sys.tables t
INNER JOIN sys.columns c
ON t.object_id = c.object_id
INNER JOIN sys.types st
ON st.system_type_id = c.system_type_id
WHERE t.name = @table

print @script + '}'

, .

+5

In Visual Studio, you can use the built-in Entity Framework "Code first from database" function to create POCO classes for use with PetaPoco. Just generate them and delete any unnecessary EF trash can, such as DBContext and attributes. Thus, you can generate POCOs not only for C #, but also for VB.NET and all visual clicks without interfering with T4 Templates.

Project / Add new item ... /Data/ADO.NET Entity Data Model / Code first from the database

0
source

All Articles