【问题标题】:Dynamic database table creation in ASP.Net Core 2 with Entity Framework使用实体框架在 ASP.Net Core 2 中创建动态数据库表
【发布时间】:2018-08-24 02:17:28
【问题描述】:

我在动态表上有问题,否则用户将在 Web 应用程序中创建表并在之后插入记录。如何在不知道用户输入的列数的情况下在运行时创建模型。 EF能支持这个问题吗?

例子

我有 1 个用于表名的文本框,然后有 1 个用于数字列的文本框 输入列数后,它将为列名动态创建文本框,然后用户将其保存。

【问题讨论】:

    标签: c# asp.net-core-mvc


    【解决方案1】:

    不确定,但恐怕你不能这样做。但你可以模拟它:

    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;}
    }
    

    我在我的项目中使用了类似的方法,并且效果很好。通过迁移可以轻松添加新类型的列。唯一的问题可能是性能 - this 是我的问题,但 EF Core 应该更快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多