简体   繁体   中英

How to Create DataTable from arrayList with Entity Framework

I have a dynamic table in my Web Application (written with angualrjs), in this table you can add columns / add rows and etc..

I want to keep this table/s stored in a database using entity framework.. I save this table to the server as a List[] (Array of List of strings - each List in the array represent a row in the table).

But entity framework requires me to create a model.. but the probelm is the table is completely dynamic.... (users can Add/Remove Rows/Columns dynamicly..)

How can I achieve storing a List[] in a data table with entity framework?

Thanks alot for the help!!!

Or is there a better way of implementing this? 在此处输入图片说明

If you want to model it in terms of EF/DB modelling, you can do 2 things(very basic)

  1. Standard one to many relationship between a row and columns.
  2. One row with id and values column, where the values column is just a comma separated list.

In the example below DynamicData and DynamicDataValue are example of 1-many relationship and DynamicData2 is comma separated list

The EF Context

public class Context : DbContext
    {
        public Context()
            : base("35213027DatatableFromArraylist"){}

        public DbSet<DynamicData> DynamicDatas { get; set; }

        public DbSet<DynamicData2> DynamicData2s { get; set; }
    }

1-many

public class DynamicData
    {
        public int Id { get; set; }

        public virtual ICollection<DynamicDataValue> Values { get; set; } 
    }

    public class DynamicDataValue
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

Comma Seperated

public class DynamicData2
    {
        public int Id { get; set; }

        public string Values { get; set; }
    }

Saving and Reading

static void Main(string[] args)
        {
            //one-many
            using (var context = new Context())
            {
                var values = new List<DynamicDataValue>();
                for (int i = 0; i < 11; i++)
                {
                    values.Add(new DynamicDataValue { Value = string.Format("Value{0}", i) });
                }

                context.DynamicDatas.Add(new DynamicData { Values = values });

                context.SaveChanges();

                var query =
                    context.DynamicDatas.Select(
                        data => new { data.Id, values = data.Values.Select(value => value.Value) }).ToList();

                foreach (var item in query)
                {
                    var strings = item.values.Aggregate((s, s1) => string.Format("{0},{1}", s, s1));
                    Console.WriteLine("{0} - {1}", item.Id, strings);
                }
            }

            Console.ReadLine();

            //comma seperated
            using (var context = new Context())
            {
                var values = new List<string> { "value1", "value2", "value3" };
                context.DynamicData2s.Add(new DynamicData2 { Values = values.Aggregate((s, s1) => string.Format("{0},{1}", s, s1)) });
                context.SaveChanges();

                var data = context.DynamicData2s.ToList();
                foreach (var dynamicData2 in data)
                {
                    Console.WriteLine("{0}-{1}", dynamicData2.Id, dynamicData2.Values);
                }
            }

        }

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