Entity Framework Create Database and Runtime Tables

I created several tables in the .edmx file and created the database by selecting "Generate Database From Model" and manually executing the .edmx.sql file in the database to build the tables.

Now, however, I am creating an installation dialog that allows the user to connect the program to their own database. I thought that context.CreateDatabase would be good enough to create a database along with tables, but tables are not created.

What is the preferred method for creating a database and tables when the user specifies his own server and database to use when they initially start with the model?

+6
source share
1 answer

Ok, I figured out a way to create tables and a database (not necessarily the best way). The result of CreateDatabaseScript can be executed:

 // create the database db.CreateDatabase(); // grab the script to create the tables string createScript = db.CreateDatabaseScript(); // execute the script on the database db.ExecuteStoreCommand(createScript); 

You should do some checking to make sure that the tables and database are not already created, otherwise the application will crash.

I'm going to take a hit to switch my project to code. I hope the above will help anyone who is watching.

+7
source

Source: https://habr.com/ru/post/919906/


All Articles