简体   繁体   中英

Dynamic database table creation in ASP.Net Core 2 with Entity Framework

I have problem on dynamic table or user will create table in the web application and will insert record after.How i can create a model on runtime without knowing number of columns inputted by user. Is EF can support this problem ?

Example

I have 1 textbox for tablename and then textbox2 for number columns after inputting number of columns it will dynamically create textbox for column names and then user will save it.

Not sure, but I am afraid you cannot do that. But you can simulate it:

Classes for CodeFirst:

public class Table
{
    public int Id{get;set;}
    public string Name {get;set;}
    public ICollection<Column> Columns {get;set;}
    public ICollection<Row> Rows {get;set;}
}

public class Row
{
    public int Id{get;set;}
    public ICollection<ColumnValueBase> Values {get;set;}
}

public abstract class Column
{
    public int Id {get;set;}
    public Column Column {get;set;}
}

public abstract class ColumnValue
{
    public int Id {get;set;}
}

public class IntColumn : Column
{
    public IntColumnValue IntValue {get;set;}
}

// other types of columns and values

public class TablesContext : DbContext
{
    public DbSet<Table> Tables {get;set;}
}

I used similar approach in my project and it works pretty well. Adding new types of columns is easy through migrations. Only problem may be performance - this is my question about it, but EF Core is supposed to be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM