Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:54:41+00:00 2026-05-26T15:54:41+00:00

I’m looking for a tutorial, guidance or software that can generate simple POCO’s for

  • 0

I’m looking for a tutorial, guidance or software that can generate simple POCO’s for some SQL Server tables for use in ASP.NET MVC. Something like this:

1) Keep a list of the table names in the SQL Server database that should have a POCO generated

2) Feed the list to some program

3) The program generates a simple POCO (more like DTO) in a single .cs file, or appends to a Poco.cs. Either way, it doesn’t matter.

For example:

public class MyDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool? IsMale {get; set;}
}

4) Run the program whenever I want to re-generate the POCO’s

The program could be WinForm, commandline, or something else. It doesn’t matter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:54:41+00:00Added an answer on May 26, 2026 at 3:54 pm

    I have wanted to answer this but been busy.

    I have created a simple example on how to read a database schema and generate classes and properties from this.

    Basically you should be able to cutnpaste this into a TT file (see Oleg Sychs blog on how to get started), update the connection string and save to execute the template.

    I don’t claim that this is a complete sample but it could serve as a starting point for you:

      <#@ template   language    = "C#"                           #>
      <#@ assembly   name        = "Microsoft.CSharp"             #>
      <#@ assembly   name        = "System.Core"                  #>
      <#@ assembly   name        = "System.Data"                  #>
      <#@ import     namespace   = "System.Collections.Generic"   #>
      <#@ import     namespace   = "System.Dynamic"               #>
      <#@ import     namespace   = "System.Linq"                  #>
      <#@ import     namespace   = "System.Data.SqlClient"        #>
    
      <#
         var namespaceName    = "Poco2";
         // Update the connection string to something appropriate
         var connectionString = @"Data Source=localhost\SQLExpress;Initial Catalog=MyTest;Integrated Security=True";
      #>
    
      <#
         using (var db = new SqlConnection (connectionString))
         using (var cmd = db.CreateCommand ())
         {
            db.Open();
            var tables              = ReadRows (cmd, "SELECT * FROM sys.tables").ToArray ();
    
            var columns             = ReadRows (cmd, "SELECT * FROM sys.columns").ToLookup (k => k.object_id);
    
            var indexes             = ReadRows (cmd, "SELECT * FROM sys.indexes").ToLookup (k => k.object_id);
            var indexColumns        = ReadRows (cmd, "SELECT * FROM sys.index_columns").ToLookup (k => k.object_id);
    
            var foreignKeys         = ReadRows (cmd, "SELECT * FROM sys.foreign_keys").ToArray ();
            var foreignKeyColumns   = ReadRows (cmd, "SELECT * FROM sys.foreign_key_columns").ToArray ();
      #>
      namespace <#=namespaceName#>
      {
         using System;
         using System.Data.Linq.Mapping;
    
      <#
            foreach (var table in tables)
            {         
      #>
         [Table]
         partial class <#=table.name#>
         {
      <#
               IEnumerable<dynamic> tc = columns[table.object_id];
               var tableColumns = tc.OrderBy (r => r.column_id).ToArray ();          
    
               IEnumerable<dynamic> ti = indexes[table.object_id];
               var tableIndexes = ti.ToArray ();          
    
               var primaryKeyIndex = tableIndexes.FirstOrDefault (i => i.is_primary_key);
               var primaryKeyColumns = new Dictionary<dynamic, dynamic> ();
               if (primaryKeyIndex != null)
               {
                  IEnumerable<dynamic> pc = indexColumns[table.object_id];
                  primaryKeyColumns = pc
                     .Where (c => c.index_id == primaryKeyIndex.index_id)
                     .ToDictionary (c => c.column_id, c => c.key_ordinal)
                     ;
               }
    
               foreach (var tableColumn in tableColumns)
               {
                  var type = MapToType (tableColumn.user_type_id, tableColumn.max_length, tableColumn.is_nullable);
    
      #>
            [Column (IsPrimaryKey = <#=primaryKeyColumns.ContainsKey (tableColumn.column_id) ? "true" : "false"#>)]
            public <#=type#> <#=tableColumn.name#> {get;set;}
    
      <#
               }
      #>
    
         }
      <#
            }
      #>
      }
      <#
         }
      #>
    
      <#+
    
         struct DataType
         {     
            public readonly int     SizeOf;
            public readonly string  SingularType;
            public readonly string  PluralType;
    
            public DataType (
               int sizeOf,
               string singularType,
               string pluralType = null
               )
            {
               SizeOf         = sizeOf;
               SingularType   = singularType;
               PluralType     = pluralType ?? (singularType + "[]");
            }
    
         }
         static Dictionary<int, DataType> dataTypes = new Dictionary<int, DataType>
            {
               {61   , new DataType (8,  "DateTime"            )},
               {127  , new DataType (8,  "long"                )},
               {165  , new DataType (1,  "byte"                )},
               {231  , new DataType (2,  "char"    ,  "string" )},
            };
    
         static string MapToType (int typeId, int maxLength, bool isNullable)
         {
            DataType dataType;
    
            if (dataTypes.TryGetValue (typeId, out dataType))
            {
               var length = maxLength > 0 ? (maxLength / dataType.SizeOf) : int.MaxValue;
               if (length > 1)
               {
                  return dataType.PluralType;
               }
               else
               {
                  return dataType.SingularType + (isNullable ? "?" : "");
               }
            }
            else
            {
               return "UnknownType_"+ typeId;
            }
         }
    
         static IEnumerable<dynamic> ReadRows (SqlCommand command, string sql)
         {
            command.CommandText = sql ?? "";
    
            using (var reader = command.ExecuteReader())
            {
               while (reader.Read())
               {
                  var dyn = new ExpandoObject ();
                  IDictionary<string, object> dic = dyn;
    
                  for (var iter = 0; iter < reader.FieldCount; ++iter)
                  {
                        dic[reader.GetName(iter) ?? ""] = reader.GetValue(iter);
                  }
    
                  yield return dyn;
               }
    
            }
         }
    
    
      #>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.