This article shows you how to build CMS website using ASP.NET MVC3, EntityFramework and SQL Server.
Case Scenario
All the samples in this serial will focus on one virtual company that produces and sales mobile phones. Three business subjects are covered: products, area (sales areas) and sales data.
Preparation
Follow the steps below to create one sample database.
Create database HiSqlServer-Sample
Create table dbo.Areas (areas of sales) using the following script:
CREATE TABLE [dbo].[Areas]
(
[AreaId] INT NOT NULL IDENTITY(100, 1) ,
[AreaName] NVARCHAR(64) NOT NULL,
CONSTRAINT [PK_Areas] PRIMARY KEY ([AreaId])
)
- Create table dbo.Products (products of the company) using the following script:
CREATE TABLE [dbo].[Products]
(
[ProductId] INT NOT NULL IDENTITY(100, 1) ,
[ProductName] NVARCHAR(64) NOT NULL,
[Cost] DECIMAL(10, 2) NOT NULL,
[Color] NCHAR(1) NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY ([ProductId])
)
- Create table dbo.Sales (sales data) using the following script:
CREATE TABLE [dbo].[Sales]
(
[SaleNo] INT NOT NULL PRIMARY KEY,
[SaleDate] DATETIME NOT NULL,
[ProductId] INT NOT NULL,
[AreaId] INT NOT NULL,
[Amount] DECIMAL(10, 2) NULL
)
- Initialize tables:
-- Three regions
INSERT INTO dbo.Areas VALUES(N'Earth');
INSERT INTO dbo.Areas VALUES(N'Mars');
INSERT INTO dbo.Areas VALUES(N'Saturn');
GO
-- R: Red; Y: Yellow
INSERT INTO dbo.Products VALUES('myPhone',2999,N'R')
INSERT INTO dbo.Products VALUES('myPhone',2999,N'Y')
INSERT INTO dbo.Products VALUES('yourPhone',1999,N'R')
INSERT INTO dbo.Products VALUES('yourPhone',1999,N'Y')
INSERT INTO dbo.Products VALUES('ourPhone',999,N'R')
INSERT INTO dbo.Products VALUES('ourPhone',999,N'Y')
- Create SQL Server Login and Database User
USE [master]
GO
CREATE LOGIN [HiSqlServer] WITH PASSWORD=N'HiSqlServer', DEFAULT_DATABASE=[HiSqlServer-Sample], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [HiSqlServer-Sample]
GO
CREATE USER [HiSqlServer] FOR LOGIN [HiSqlServer] WITH DEFAULT_SCHEMA=[dbo]
GO
USE [HiSqlServer-Sample]
GO
ALTER AUTHORIZATION ON SCHEMA::[db_owner] TO [HiSqlServer]
GO
ALTER ROLE [db_owner] ADD MEMBER [HiSqlServer]
Create Website to Manage Product (dbo.Products)
- Create website project “HiSqlServer-Sample”
/project/allsqlserver/resources/31725C46-2F61-56A4-9462-B588FFC1472C.webp
2)Create entity framework model SalesRepository.edmx
/project/allsqlserver/resources/A6AB80DF-9340-524F-95FE-83A9B586CA82.webp
Choose “Generate from database”:
/project/allsqlserver/resources/2322A9DA-7113-56A6-8EFD-35E067166C05.webp
Create new connection to the database HiSqlServer-Sample:
/project/allsqlserver/resources/55A185C8-30FF-5DB0-B673-4847CA3269FF.webp
Keep the connection string in the configuration file:
/project/allsqlserver/resources/4E69F0A3-9493-5A65-9DDE-BA9B407D3FEB.webp
Choose the tables, set model namespace as ‘SalesModel’ and then click Finish to save.
/project/allsqlserver/resources/FD22F925-DFCA-5E19-B750-7C3AD811AC19.webp
The model looks like the following screenshot:
/project/allsqlserver/resources/0DA387D6-C8F5-526A-BDEE-5ABA7069404D.webp
- Create new controller ‘ProductController’ using the entity model
/project/allsqlserver/resources/82E8D28B-AD16-5F73-B3D7-A1E4B8E86DE3.webp
Visual Studio will automatically generate the codes and views for managing the product entity, including Create, Edit. Delete and Details.
/project/allsqlserver/resources/C6B9959A-2057-5E69-9E68-F1A4CC65BDD0.webp
/project/allsqlserver/resources/29BFBB31-8E9C-59BF-AB3C-F172BE63BCC4.webp
Using MVC + Entity Framework + SQL Server, we can complete the management website page just in several minutes.