Generate C# class from SQL Server table without Store Procedure
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
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:
-
Make a
select
as json query:SELECT * FROM Approval FOR JSON AUTO
-
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"} ]
-
Paste in Visual Studio:
Edit
>Paste Special
>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
or for simple single queries:
https://visualstudiomagazine.com/articles/2012/12/11/sqlqueryresults-code-generation.aspx
Related videos on Youtube
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, 2021Comments
-
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?
public class Approval { public long Id { get; set; } public long? FormId { get; set; } public long? DesignationId { get; set; } public DateTime? CreatedOn { get; set; } }
-
Budyn almost 6 yearsyou can use database first approach which will create a diagram and then all classes
-
Dan Wilson almost 6 yearsWhat problem are you actually trying to solve? You have already created a class to model the table you show.
-
bugnuker almost 6 years
-
-
MTAdmin over 5 yearsJSON is only supported in MSSQL 2016+. But Paste as XML works great in MSSQL 2008+.
-
c-sharp-and-swiftui-devni almost 5 yearsGreat 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 almost 5 yearsam on 2019 have they removed this now ?.
-
c-sharp-and-swiftui-devni almost 5 yearscan be quite buggy and often crash.
-
Robert Stokes over 4 yearsAwesome! I didn't feel like typing out a really long class. This tool saved me 10 min!
-
Waqas Nawaz Warraich over 4 yearsIf you do not want to use online tool ,use store procedure in following post stackoverflow.com/questions/5873170/…
-
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 over 4 years@jonathana no doubt your tool is very helpful and user friendly, Well done
-
Sparrow over 4 yearsbigint nullable columns are not generated with "?"
-
Kijana Woodard almost 4 years@rogue39nin it's still under Edit in VS2019, but not there if you "right-click".
-
RoLYroLLs almost 4 yearsThis 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 over 3 yearsGreat tool. Added to my favorites.