Last post Nov 25, 2013 05:49 AM by Starain chen - MSFT
Nov 22, 2013 12:06 AM|carlg2|LINK
I've been following the ContosoUniversity sample that is posted on this site, but I've been using my own models.
I set up all of my models and created a new controller. The scaffolding nicely created my controller and all of my views. But, for some reason it did not create any of my associated database tables for me.
Now that I'm at this point is there a way I can easily run something to create all of my database tables for me using my models?
Nov 22, 2013 07:50 AM|susantamukherjee|LINK
Do you have a context class dervied from DbContext where you will be passing your connectionstring name?
Nov 22, 2013 12:24 PM|carlg2|LINK
"Do you have a context class dervied from DbContext where you will be passing your connectionstring name?
For some reason, I couldn't get the scaffolding to create my database tables for me. I do have an existing database, because when I created my project, I chose to have security implemented. This created all of the security tables for me successfully.
Using the security/user log in features is about the only thing I did differently then the example.
But then my expectation was that when I added my first controller, it would create the tables for me. It did not. But it did create my controller with all of the views for me. I guess I can easily go in and create all of the db tables manually using SQL
(create table...). I just thought it would be cool to see the scaffolding do this for me.
Nov 22, 2013 01:39 PM|susantamukherjee|LINK
As you mentioned you have the database already then you probably need to use code first database migration or you can try implementing the DropCreateDatabaseIfModelChanges class under System.Data.Entity. You can check below article :
Nov 22, 2013 04:28 PM|imran_ku07|LINK
This might help you,
Nov 25, 2013 05:49 AM|Starain chen - MSFT|LINK
Thanks for your post!
Based on my test, I find it will not generate the database when we create controller and views via scaffolding.
A way to generate the database is during it retrieve data from database (Entity framework code first), such as:
UsersContext db = new UsersContext();
db.UserProfiles.ToList();//It will generate the database