Page 1 of 1

Create C# Model From TSQL Table

Posted: Thu Oct 21, 2021 1:35 am
by jussij
The attached zip file contains a TSQL stored procedure that can be used to create a C# Model for a given TSQL table.

usage: EXEC [dbo].[sp_ModelFromTable] tblExample

Running that stored procedure will produced the model shown below:

Code: Select all

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Database.Model
{
    [Table("tblExample", Schema = "dbo")]
    public class tblExampleModel
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        Column("ID")]
        public int ID { get; set; }
 
        Column("Name")]
        public string Name { get; set; }
    }
}