Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>I have wanted to answer this but been busy.</em></p> <p>I have created a simple example on how to read a database schema and generate classes and properties from this.</p> <p>Basically you should be able to cutnpaste this into a TT file (see <a href="http://www.olegsych.com/2007/12/text-template-transformation-toolkit/" rel="noreferrer">Oleg Sychs blog</a> on how to get started), update the connection string and save to execute the template.</p> <p>I don't claim that this is a complete sample but it could serve as a starting point for you:</p> <pre><code> &lt;#@ template language = "C#" #&gt; &lt;#@ assembly name = "Microsoft.CSharp" #&gt; &lt;#@ assembly name = "System.Core" #&gt; &lt;#@ assembly name = "System.Data" #&gt; &lt;#@ import namespace = "System.Collections.Generic" #&gt; &lt;#@ import namespace = "System.Dynamic" #&gt; &lt;#@ import namespace = "System.Linq" #&gt; &lt;#@ import namespace = "System.Data.SqlClient" #&gt; &lt;# var namespaceName = "Poco2"; // Update the connection string to something appropriate var connectionString = @"Data Source=localhost\SQLExpress;Initial Catalog=MyTest;Integrated Security=True"; #&gt; &lt;# 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 =&gt; k.object_id); var indexes = ReadRows (cmd, "SELECT * FROM sys.indexes").ToLookup (k =&gt; k.object_id); var indexColumns = ReadRows (cmd, "SELECT * FROM sys.index_columns").ToLookup (k =&gt; k.object_id); var foreignKeys = ReadRows (cmd, "SELECT * FROM sys.foreign_keys").ToArray (); var foreignKeyColumns = ReadRows (cmd, "SELECT * FROM sys.foreign_key_columns").ToArray (); #&gt; namespace &lt;#=namespaceName#&gt; { using System; using System.Data.Linq.Mapping; &lt;# foreach (var table in tables) { #&gt; [Table] partial class &lt;#=table.name#&gt; { &lt;# IEnumerable&lt;dynamic&gt; tc = columns[table.object_id]; var tableColumns = tc.OrderBy (r =&gt; r.column_id).ToArray (); IEnumerable&lt;dynamic&gt; ti = indexes[table.object_id]; var tableIndexes = ti.ToArray (); var primaryKeyIndex = tableIndexes.FirstOrDefault (i =&gt; i.is_primary_key); var primaryKeyColumns = new Dictionary&lt;dynamic, dynamic&gt; (); if (primaryKeyIndex != null) { IEnumerable&lt;dynamic&gt; pc = indexColumns[table.object_id]; primaryKeyColumns = pc .Where (c =&gt; c.index_id == primaryKeyIndex.index_id) .ToDictionary (c =&gt; c.column_id, c =&gt; c.key_ordinal) ; } foreach (var tableColumn in tableColumns) { var type = MapToType (tableColumn.user_type_id, tableColumn.max_length, tableColumn.is_nullable); #&gt; [Column (IsPrimaryKey = &lt;#=primaryKeyColumns.ContainsKey (tableColumn.column_id) ? "true" : "false"#&gt;)] public &lt;#=type#&gt; &lt;#=tableColumn.name#&gt; {get;set;} &lt;# } #&gt; } &lt;# } #&gt; } &lt;# } #&gt; &lt;#+ 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&lt;int, DataType&gt; dataTypes = new Dictionary&lt;int, DataType&gt; { {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 &gt; 0 ? (maxLength / dataType.SizeOf) : int.MaxValue; if (length &gt; 1) { return dataType.PluralType; } else { return dataType.SingularType + (isNullable ? "?" : ""); } } else { return "UnknownType_"+ typeId; } } static IEnumerable&lt;dynamic&gt; ReadRows (SqlCommand command, string sql) { command.CommandText = sql ?? ""; using (var reader = command.ExecuteReader()) { while (reader.Read()) { var dyn = new ExpandoObject (); IDictionary&lt;string, object&gt; dic = dyn; for (var iter = 0; iter &lt; reader.FieldCount; ++iter) { dic[reader.GetName(iter) ?? ""] = reader.GetValue(iter); } yield return dyn; } } } #&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload