Welcome back, this is part 2 of the MVC 3 / Razor storefront series. In this post, we will set up the project, create the database structure and set up some folders.
To begin, I will assume that you have installed the MVC 3 Release Candidate bits. If not, you can get it at http://bit.ly/h1hGym My development environment is Visual Studio.NET Ultimate 2010. You will need .NET 4 (Framework), ASP.NET 4 and Visual Studio.NET 2010 or Visual Web Developer 2010 to develop this particular solution. As part of the MVC 3 installation, you will also get the wonderful open source, package manager, NuGet. This greatly simplifies the installation of extensions (and we may need some). Also, I am using SQL Server 2008 Express Edition for this solution.
Okay, let’s get going…
1. Creating the project
Create a new project and choose the “ASP.NET MVC 3 Web Application” template as shown below.
2. Choose the project template
Clicking on OK will launch the choice dialog box for selecting the project template as shown below.
Go ahead, and choose the “Internet Application” template. You may additionally check the “Create a unit test project” check box but I will skip TDD for this project.
2. Creating the MembershipProvider support
Now run the application by hitting the F5 key and it should launch under the local ASP.NET Development server over a non-standard port. Viola! Here is your application with the ability to login, and register.
Now, register a new user by first clicking on the “Log On” link at the top right corner of your application and then on the “Register” link in the subsequent page. I created a user called Admin for now.
Clicking on the Register button will in turn create a database called ASPNETDB under the App_Data folder. Let us add this to our solution explorer by right clicking the App_Data folder and adding an existing item and choosing the ASPNETDB.MDF file to be added. You should now be able to double click on the ASPNETDB database under the solution explorer and have it appear under the “Data Connections” window.
This will also create a connection string called “ApplicationServices” in the web.config to the default ASPNETDB database to support the MembershipProvider. In fact, the Membership, Profile and roleManager providers all point to this connection. For this project, we will add our own store schema to the same database. (I may cover how to use your own custom Membership provider with your own database or you may look it up on the internet).
3. Creating the database schema
We will now add the required tables to the database. Specifically we will add the following:
The entire database is attached and you may download it.
4. Creating the Entity Framework Model
Now we will create an entity framework model from our database schema. To do this, we will
1. Right click on the Models folder within the Solution Explorer and click on add… new item.
2. Click on Add and then choose “Generate from Database”
3. In the Entitiy Data Model Wizard, (which like a wizard recognizes your database from the connection settings defined in the web.config), change the entity connection settings name to MystoreEntities and click on Next.
4. In the “Choose Your Database Objects” step, select the following tables and change the Model Namespace to “MystoreModel”.
Cart, Category, Customer, Order, OrderItems, Product as shown below.
5. Now click on Finish to close the wizard and have the EF object model created. You will now see the object model generated as the edmx file as shown below.
Conclusion and next steps…
So, we have just created a blank MVC 3 / Razor solution, added a database with membership and role support and support for the online store. We then exposed this as an object data model using the Entity Framework.
Next time, we will create some templates, add validation at the model level and showcase some (featured) products on the home page using the model-view-controller.
Until next time, take care…
App_Data.zip (234.58 kb)