none
How to create Data Table during runtime using entity framework (datasource = (localdb)\v11.0) RRS feed

  • Question

  • How to create a new table during runtime and add it a database? Provider: Microsoft sql server.

    Or

    How to create a new Table based on another table. For example I have got a table with x and y as column names, and table1 as table's name in a Database. During runtime how to create a new Table based on table1 (but empty) and name it table2 in the same database.

    I am starting to think that it's impossible to do it as entity framework creates classes for each Table!

    Wednesday, December 17, 2014 10:40 PM

Answers

  • Hello Bopy,
    >>How to create a new table during runtime and add it a database? Provider: Microsoft sql server.
    By default, Entity Framework is not designed to do such a thing, it do more things regarding ORM. However, since Entity Framework is based on ADO.NET, it also exposes the function to execute the raw sql setatment, so you could write sql statement to create table by using ExecuteSqlCommand as:
    using (CFContext db = new CFContext())
                    {
                        db.Database.ExecuteSqlCommand("your sql statement to create tables");
                    }
    However, the newly created table is not accessible for the Entity Framework since we cannot drop the new table from database to designed model windows at run time(this should is done at designed time), we still use the ADO.NET way by the ExecuteSqlCommand method to access the new table.
    Regards.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 19, 2014 1:17 AM
    Moderator