16

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; }
}
  • 1
    you can use database first approach which will create a diagram and then all classes – Budyn Sep 07 '18 at 15:22
  • 1
    What problem are you *actually* trying to solve? You have already created a class to model the table you show. – Dan Wilson Sep 07 '18 at 15:23
  • I am using store procedure to get data and in razor view i want to refer a class of store procedure like @model Brass.Models.EmployeeClass so i want a easy way to generate class dont want to use long procedure like code first approch – Waqas Nawaz Warraich Sep 07 '18 at 15:24
  • @MaxVernon: Does Dapper have a tool to create a POCO type from a table or query? I had a tool I wrote in a previous job (that extracted metadata from a Data Reader), but, alas, I don't work there anymore. – Flydog57 Sep 07 '18 at 15:57
  • I'm curious about this too, but for stored procedures. I'd like to somehow return the column data types from a SP and turn it into a POCO. – jon.r Sep 07 '18 at 16:20
  • @Flydog57 - actually I don't think it does. I'll remove my comment. – Hannah Vernon Sep 07 '18 at 16:26
  • 2
    Look here: https://gist.github.com/halityurttas/17e36d2fa047b925ccef7940d451083e – bugnuker Sep 07 '18 at 16:36
  • Copy 1 Row of output (with header) and paste here to [Generate Class from CSV](https://toolslick.com/generation/code/class-from-csv) – KyleMit Aug 26 '20 at 01:56
  • Open Package Manager Console and type this: `Scaffold-DbContext "Server=servername.com;Database=My_Db;Trusted_Connection=True;MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir "Models/Entities" -DataAnnotations -Tables Approval`. Source: https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding/?tabs=vs. – Ash K Dec 19 '22 at 21:56

3 Answers3

59

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

Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
29

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Alpha75
  • 2,140
  • 1
  • 26
  • 49
11

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

Tomasito
  • 1,864
  • 1
  • 20
  • 43