I'm just getting to grips with MVC linq etc etc and came across what looks like a common stumbling block. For the more experienced of you reading this please refrain from shouting "Dumbass" at me.
All the tutorials are either Code First examples or they create the database from scratch in the App_Data directory. All well and good for a tutorial that need to be easily portable to the readers computer, but not very helpful when setting up a full scale MVC application.
My first problem was my lack of knowledge of Linq to SQL. If you fall into this category have a look at this link - it's gold dust.
Finally, how to add an external SQL database to your MVC project:
- Right click on "Models" folder, select "Add New Item"
- Add a "Link to SQL Classes" item
- Open your "Server Explorer" pane (if you cant see it try "View" on the menu bar and "Server Explorer"
- Right click on "Data Connections" and select "Add Connection"
- Follow the instructions.
- Almost there....
- Expand your newly added database to view the tables.
- Drag the tables you want over to the main pane of the "Link to SQL Classes" item you added at the start.
- Hey presto, you have a database context you can run Linq queries against.
Please bear in mind you will need to use the "Models" namespace to reference you database context objects.
And now back to highly sophisticated programming!