views:

29

answers:

1

Is it possible to create a new table programatically in EF or L2S and load it to its context?

We have this application that requires to create a table at runtime and do some CRUD activities to that table.

Is this possible? If not, any advice?

+1  A: 

You can do this with EF 4 and Code First.

Craig Stuntz
Code first can create tables base on your model. but it seems you cannot add additional tables or modify them after the database is created.
mcxiand
@mcxiand: That is wrong. Look at step 5 in [this demo](http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx).
Craig Stuntz
Its still not what im looking for. You can update your table base on your code but you will lose data. It is noted that data migration for that said feature is not yet included on the CTP. “RecreateDatabaseIfModelChanges” behavior isn’t intended for production scenarios where you want to “migrate” existing data from one schema to another. Instead it is designed for development scenarios where you want the database to be quickly and automatically updated for you – without you having to take any manual steps or specify migration rules to do so.
mcxiand
You didn't say that you needed to preserve existing data in your question. The only way to do that is to use the "Database Generation Power Pack" which works with VS 2010 DB deployment projects to do DB schema compares and generate change scripts. It's significantly more complicated, but it works.
Craig Stuntz
Sorry, i forgot to mention that i needed it to preserve the existing data while doing update on table structures. I will try that one. Thanks
mcxiand