Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c# - Linq2DB Create dynamic table model

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 :)

question from:https://stackoverflow.com/questions/65890886/linq2db-create-dynamic-table-model

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...