YAcaCsh YAcaCsh - 12 days ago 5
SQL Question

Automatically create SQL tables and columns using C# & Entity Framework

I want to create different tables in SQL database using for loop in C# with Entity Framework. In addition, I want to add many columns in each table based on the calculation functions in each loop.

For example,

I have a C# class contains 50 loops. In each loop should do some calculations and add values to a table.

Each loop with each new table have individually different number of columns.

How can you be more flexible with proceeding SQL data using C# & Entity Framework?

It is like auto load entities after each for loop?

Answer

If I understand you correctly, You want to generate Tables dynamically during runtime using Entity Framework, Correct? If so then I have to say that is not going to happen. At least in the context of a reasonable application development cycle. You could probably hack some sort of solution using Roslyn, out of process calls etc. But the effort would far outweigh the eventual benefit.

You can dynamically create tables and associated columns using direct DDL but EF will not know about them. And maintaining awareness of those tables and columns between runtime sessions would be problematic.

Perhaps you would do better to explain the business problem you are trying to solve and we can collectively develop a more reasonable problem solution.