Generate C# class from SQL Server table without Store Procedure

34,706

Solution 1

This online tool generates a class from SQL table. Class is based on the CREATE TABLE script in MS-SQL, ORACLE, MYSQL, POSTGRESQL and SQLite databases, to a class in C# and other programming languages.

https://codverter.com/src/sqltoclass

enter image description here

Solution 2

If you want to use Entity Framework you should consider to use Database First approach.

Other simple and fast way to import a Sql Server scheme to a class in VS is this:

  1. Make a select as json query:

    SELECT * FROM Approval 
    FOR JSON AUTO
    
  2. Copy one row from the result in your clipboard:

    [
      {"Id":1, "FormId":10, "DesignationId":100, "CreatedOn":"2000-01-01T00:00:00"},
      {"Id":2, "FormId":20, "DesignationId":200, "CreatedOn":"2000-01-01T00:00:00"}
    ]
    
  3. Paste in Visual Studio: Edit > Paste Special > Paste JSON As Classes

    Paste JSON as Classes

Solution 3

For tables, stored procedures, etc., when you have many POCO to generate:

https://www.codeproject.com/Articles/892233/POCO-Generator

POCO generator

or for simple single queries:

https://visualstudiomagazine.com/articles/2012/12/11/sqlqueryresults-code-generation.aspx

Simple POCO generator

Share:
34,706

Related videos on Youtube

Waqas Nawaz Warraich
Author by

Waqas Nawaz Warraich

“I’m currently working as a .Net Developer in Sharjah Charity UAE. I've 4 years of experience working .Net framework including 1 year in SAP add-on development.I consider myself a passionate developer, and I have a good sense of humor. ”

Updated on January 12, 2021

Comments

  • Waqas Nawaz Warraich
    Waqas Nawaz Warraich over 3 years

    Is there any way to generate a class for table in SQL Server without adding table in project with ADO.net Entity Framework?

    enter image description here

    public class Approval
    {
            public long Id { get; set; }
            public long? FormId { get; set; }
            public long? DesignationId { get; set; }
            public DateTime? CreatedOn { get; set; }
    }
    
    • Budyn
      Budyn almost 6 years
      you can use database first approach which will create a diagram and then all classes
    • Dan Wilson
      Dan Wilson almost 6 years
      What problem are you actually trying to solve? You have already created a class to model the table you show.
    • bugnuker
      bugnuker almost 6 years
  • MTAdmin
    MTAdmin over 5 years
    JSON is only supported in MSSQL 2016+. But Paste as XML works great in MSSQL 2008+.
  • c-sharp-and-swiftui-devni
    c-sharp-and-swiftui-devni almost 5 years
    Great we find I was using a .net desktop app but it always froze i love these online tools nice work if it is use.
  • c-sharp-and-swiftui-devni
    c-sharp-and-swiftui-devni almost 5 years
    am on 2019 have they removed this now ?.
  • c-sharp-and-swiftui-devni
    c-sharp-and-swiftui-devni almost 5 years
    can be quite buggy and often crash.
  • Robert Stokes
    Robert Stokes over 4 years
    Awesome! I didn't feel like typing out a really long class. This tool saved me 10 min!
  • Waqas Nawaz Warraich
    Waqas Nawaz Warraich over 4 years
    If you do not want to use online tool ,use store procedure in following post stackoverflow.com/questions/5873170/…
  • Jonathan Applebaum
    Jonathan Applebaum over 4 years
    @WaqasNawazWarraich Thank you for the link. BTW, from a security point of view, All the data entered to the editors is interpreted and converted only on the client-side (using Angular and Typescript) and never sent back to the server (full disclosure - I am the developer of this tool).
  • Waqas Nawaz Warraich
    Waqas Nawaz Warraich over 4 years
    @jonathana no doubt your tool is very helpful and user friendly, Well done
  • Sparrow
    Sparrow over 4 years
    bigint nullable columns are not generated with "?"
  • Kijana Woodard
    Kijana Woodard almost 4 years
    @rogue39nin it's still under Edit in VS2019, but not there if you "right-click".
  • RoLYroLLs
    RoLYroLLs almost 4 years
    This works great but it doesn't work until you have data. I just built the table and I don't want to go through the trouble of adding data.
  • John Prado
    John Prado over 3 years
    Great tool. Added to my favorites.

Related