0

Is it possible to create a table model that depends on the user input ? Globally I'll get a List containing the column names of the table that I should create in the DB. The Question is how can I manage that as I don't know how many columns I must create.

The standard approach for a defined table (known structure) would be something like this:

[Table("TableName")]
public class TableName
{
   [PrimaryKey, Identity] public int Id;
   [Column] public string Name;
}

public void CreateTable(string configString)
{
   using (var db = new DataConnection(configString))
   {
      db.CreateTable<TableName>();
   }
}

Is it possible to create the table model dynamically, from something like this:

var userInput = new List<string>() { "Name", "Firstname", "Adresse", "Zipcode", "City" };

To get a model like this for the table creation:

[Table("TableName")]
public class TableName
{
   [PrimaryKey, Identity] public int Id;
   [Column] public string Name;
   [Column] public string Firstname;
   [Column] public string Adresse;
   [Column] public string Zipcode;
   [Column] public string City;
} 

Thanks for any help :)

CrazyNoun
  • 23
  • 5
  • 1
    Actually for now it is no way to do that without code change. DDL SQL generation is not a main purpose of the library. `CreateTable` was basically introduced for helping in creating temporary tables when classes are known. There possible way to generate classes dynamically, but I do not think that it worth time. – Svyatoslav Danyliv Jan 26 '21 at 09:11
  • Okay, thanks for the answer. I'm a bit disappointed, but it's still a great ORM for the rest. – CrazyNoun Jan 26 '21 at 13:00
  • Actually this was requested earlier, but no time for implement. It needs new DDL API, like AddColumn, RenameColumn, DropColumn, ect. – Svyatoslav Danyliv Jan 26 '21 at 14:03

0 Answers0