I have been trying to follow the few tutorials on how to create a DB using the model first approach in MVC. I have gone as far as creating Entities in the designer and trying to generate a DB from the model. It creates SQL file fine but I cant seem to execute it? I have no option to do so? When I right click the file the execute option is not available and when I open the file and right click in the context the option is not available. I have tried connecting to a blank DB and then creating the Entity Model on this DB with model entities and then trying to generate a DB from the model but still no option to execute the sql from within VS2010 express. Is there a reason why its not there ?
Question 2 I like using the model first approach as I find the graphical representation of all the entities and their relationships easy to understand and I like the fact all the C# Db access classes get created at a click of a button. However most examples for MVC 3 seem to use the code first approach. are there big benefits in using code first over model first? which approach is easier to grasp ? I come from DB background and when i did do some asp coding way back, DBs were always in place first :)
i watched model first tutorials and i couldn't right click and execute the sql generated from the entity data model. I found an answer online. I had to (1) remove the keyword "GO" from throughout the sql file, then (2) remove the line at the top that says "USE ... blah blah", then (3) right click on the database mdf in the database explorer window and click "new query" (4) replace the words "select from blah blah" in the new query window with my revised sql code. (5) when I right clicked in the new query window, NOW I had the option to execute sql. It gave a warning but then said it completed successfully (and it worked just like in the video after all of that). There is a better walkthrough with pictures in the article I found http://www.telnet80.com/2010/09/fixing-entity-framework-model-first-sql.html
Model first is way cooler than code first in my opinion, but only if there ends up being an easy way for beginners like me to re-seed the data when they end up tweaking the data model and regenerating the database. The only examples I've seen are code first.
Ad 2: There are no big benefits in any of three possible ways how to work with EF (code first, db first, model first). They exist to fit different scenarios and different users afaik.
I personally like code first when starting new project, because I can prepare my entites faster by writing classes. It's nice and fast way for prototyping, but it starts to be a bit harder once you need to create more complex models. You have to touch FluentAPI and learn more about conventions etc.
Just choose which one you like more, or which one fits the situation best (ie. if you already have database ready, or you are starting from blank).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With