Create SQL Server table from custom class

I am creating a mini ORM to learn more about reflection in C #.

I also want to use Native and Pure C # and not use Entity Framework or SQL Server Management Objects .

public static void CreateTable(Type tableType, string connectionString)
{
     // codes to create table in database
}

The problem is the method CreateTable, it has a lot of problems, for example:

  • I could not find a standard way to create a table from a custom class. How to create database tables from C # classes? : This solution is good, but not perfect. It may fail in some situations.

  • Datatypes!
    There is no integration between C # and SQL Server data types. For example, varchar, nvarcharand textare all (or matched) string!

So, when the user creates this class:

public class Person
{        
   public int ID { get; set; }        
   public string Name { get; set; }
}

What type of SQL Server data should I use for Name?

Finally, if you generate an SQL query and execute it, this is the best way for this question, what is your suggestion?

+4
source share
1 answer

What type of SQL should be used for the name ???

If you look at the Entity Framework as an example, to follow, by default they display System.Stringin NVARCHAR(MAX)all cases. Using property attributes in the first model of the code entity (for example, [Column(DbType = "CHAR(10)")]allows the developer to explain the types of columns.

, NVARCHAR(MAX) ORM . .

+3

Source: https://habr.com/ru/post/1673291/


All Articles