http://blog.sina.com.cn/s/blog_3d25ef340101aozv.html
1、搭建MVC框架
1.1 VS2010:需要安装安装WPI 安装 ASP.NET MVC 4 和Visual Studio 2010 系统必备组件
如果上述链接无法打开,请访问:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
如果VS2010 创建Web项目时,出现ASP.NET MVC 4 Web Application ,说明组件安装成功。
1.2 VS2012:无需安装任何组件
2、搭建EF框架
2.1 创建一个数据库OumindBlog
USE [OumindBlog]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Enumeration](
[e_id] [int] IDENTITY(1,1) NOT NULL,
[e_ename] [varchar](50) NOT NULL,
[e_cname] [varchar](50) NOT NULL,
[e_type] [varchar](50) NOT NULL,
[e_remark] [varchar](200) NOT NULL,
[e_addtime] [datetime] NOT NULL,
[e_delflag] [bit] NOT NULL,
CONSTRAINT [PK_enumeration] PRIMARY KEY CLUSTERED
(
[e_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'枚举表id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Enumeration', @level2type=N'COLUMN',@level2name=N'e_id'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'枚举英文名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Enumeration', @level2type=N'COLUMN',@level2name=N'e_ename'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'枚举中文名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Enumeration', @level2type=N'COLUMN',@level2name=N'e_cname'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父枚举id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Enumeration', @level2type=N'COLUMN',@level2name=N'e_type'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[BlogUser](
[Id] [int] IDENTITY(1,1) NOT NULL,
[LoginName] [nvarchar](50) NOT NULL,
[LoginPwd] [char](32) NOT NULL,
[CnName] [nvarchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
[IsLock] [int] NOT NULL,
[IsDel] [bit] NOT NULL,
[AddTime] [datetime] NOT NULL,
[LastLoginTime] [datetime] NOT NULL,
CONSTRAINT [PK_BlogUser] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户表主键ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'Id'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'登录名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'LoginName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'密码(md5_取16位)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'LoginPwd'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'中文名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'CnName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'邮件地址' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'Email'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'锁定标志' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'IsLock'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'删除标志' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'IsDel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'新增时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'AddTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后登录时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogUser', @level2type=N'COLUMN',@level2name=N'LastLoginTime'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BlogArticleCate](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Author] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Remark] [nvarchar](200) NULL,
[Statu] [int] NOT NULL,
[IsDel] [bit] NOT NULL,
[Addtime] [datetime] NOT NULL,
CONSTRAINT [PK_blog_article_cate] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'文章类别表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Id'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'作者id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Author'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'博客分类名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Name'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'博客分类介绍' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Remark'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'状态 1-公开2-隐藏3-好友公开' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Statu'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'删除标志' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'IsDel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'新增时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticleCate', @level2type=N'COLUMN',@level2name=N'Addtime'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BlogArticle](
[AId] [int] IDENTITY(1,1) NOT NULL,
[ACate] [int] NOT NULL,
[AAuthor] [int] NOT NULL,
[ATitle] [nvarchar](100) NOT NULL,
[AContent] [ntext] NOT NULL,
[AImgsrc] [nvarchar](200) NULL,
[APlnum] [int] NOT NULL,
[AAllowPL] [bit] NOT NULL,
[AIsTop] [bit] NOT NULL,
[ATag] [nvarchar](50) NULL,
[ACick] [int] NOT NULL,
[AStatu] [int] NOT NULL,
[AAddtime] [datetime] NOT NULL,
[AUpdatetime] [datetime] NOT NULL,
[AIsDel] [bit] NOT NULL,
[AHtmlSrc] [nvarchar](50) NULL,
CONSTRAINT [PK_blog_article] PRIMARY KEY CLUSTERED
(
[AId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'文章表ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'所属类别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'ACate'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'作者' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AAuthor'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'标题' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'ATitle'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'文章内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AContent'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'评论数' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'APlnum'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否允许评论' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AAllowPL'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'是否置顶' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AIsTop'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'文章标签' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'ATag'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'点击次数' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'ACick'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'状态 1-公开2-隐藏3-好友公开' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AStatu'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AAddtime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AUpdatetime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'删除标志' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AIsDel'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用来保存这篇文章的静态页面地址' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogArticle', @level2type=N'COLUMN',@level2name=N'AHtmlSrc'
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_BlogArticle_APlnum] DEFAULT ((0)) FOR [APlnum]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_blog_article_AllowPL] DEFAULT ((1)) FOR [AAllowPL]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_blog_article_IsTop] DEFAULT ((0)) FOR [AIsTop]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_BlogArticle_ACick] DEFAULT ((0)) FOR [ACick]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_blog_article_Satu] DEFAULT ((2)) FOR [AStatu]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_blog_article_Addtime] DEFAULT (getdate()) FOR [AAddtime]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_BlogArticle_Updatetime] DEFAULT (getdate()) FOR [AUpdatetime]
GO
ALTER TABLE [dbo].[BlogArticle] ADD CONSTRAINT [DF_blog_article_IsDel] DEFAULT ((0)) FOR [AIsDel]
GO
ALTER TABLE [dbo].[BlogArticleCate] ADD CONSTRAINT [DF_blog_article_cate_Statu] DEFAULT ((1)) FOR [Statu]
GO
ALTER TABLE [dbo].[BlogArticleCate] ADD CONSTRAINT [DF_blog_article_cate_Delflag] DEFAULT ((0)) FOR [IsDel]
GO
ALTER TABLE [dbo].[BlogArticleCate] ADD CONSTRAINT [DF_BlogArticleCate_Addtime] DEFAULT (getdate()) FOR [Addtime]
GO
ALTER TABLE [dbo].[BlogUser] ADD CONSTRAINT [DF_BlogUser_IsLock] DEFAULT ((0)) FOR [IsLock]
GO
ALTER TABLE [dbo].[BlogUser] ADD CONSTRAINT [DF_BlogUser_IsDel] DEFAULT ((0)) FOR [IsDel]
GO
ALTER TABLE [dbo].[BlogUser] ADD CONSTRAINT [DF_BlogUser_AddTime] DEFAULT (getdate()) FOR [AddTime]
GO
ALTER TABLE [dbo].[BlogUser] ADD CONSTRAINT [DF_BlogUser_LastLoginTime] DEFAULT (getdate()) FOR [LastLoginTime]
GO
ALTER TABLE [dbo].[Enumeration] ADD CONSTRAINT [DF_enumeration_e_addtime] DEFAULT (getdate()) FOR [e_addtime]
GO
ALTER TABLE [dbo].[Enumeration] ADD CONSTRAINT [DF_enumeration_e_delflag] DEFAULT ((0)) FOR [e_delflag]
GO
ALTER TABLE [dbo].[BlogArticle] WITH CHECK ADD CONSTRAINT [FK_BlogArticle_BlogArticleCate] FOREIGN KEY([ACate])
REFERENCES [dbo].[BlogArticleCate] ([Id])
GO
ALTER TABLE [dbo].[BlogArticle] CHECK CONSTRAINT [FK_BlogArticle_BlogArticleCate]
GO
ALTER TABLE [dbo].[BlogArticle] WITH CHECK ADD CONSTRAINT [FK_BlogArticle_BlogUser] FOREIGN KEY([AAuthor])
REFERENCES [dbo].[BlogUser] ([Id])
GO
ALTER TABLE [dbo].[BlogArticle] CHECK CONSTRAINT [FK_BlogArticle_BlogUser]
GO
ALTER TABLE [dbo].[BlogArticle] WITH CHECK ADD CONSTRAINT [FK_BlogArticle_Enumeration] FOREIGN KEY([AStatu])
REFERENCES [dbo].[Enumeration] ([e_id])
GO
ALTER TABLE [dbo].[BlogArticle] CHECK CONSTRAINT [FK_BlogArticle_Enumeration]
GO
ALTER TABLE [dbo].[BlogArticleCate] WITH CHECK ADD CONSTRAINT [FK_BlogArticleCate_BlogUser] FOREIGN KEY([Author])
REFERENCES [dbo].[BlogUser] ([Id])
GO
ALTER TABLE [dbo].[BlogArticleCate] CHECK CONSTRAINT [FK_BlogArticleCate_BlogUser]
GO
2.2 用VS2010创建一个MVC程序
2.3 创建OumindBlog.edmx文件
路径:项目->添加新项
选择 数据 - ADO.NET 实体数据模型,然后把名称改为 OumindBlog
发现View文件夹下多了一个OumindBlog.edmx 文件
2.4 添加 EntityFrame 程序包
路径 :项目 ->管理NuGet程序包
首先,需要从 NuGet 获取实体框架的最新版本。
如果链接无法打开,请访问:http://msdn.microsoft.com/zh-cn/data/jj206878 或者http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
点击“安装”
安装已完成
如果EF程序包已安装,再次打开安装,可以看到程序包右上角有个绿色的对勾,说明程序包已安装。
2.5 添加 EF 5.x DbContext Generator for C# 模板
在 EF 设计器中,右键单击模型上的空位置,选择“添加代码生成项...”
选择“EF 5.x DbContext Generator for C#”,输入名称 OumindBlog,然后单击“添加”
从左侧菜单中选择“联机模板”,然后搜索“DbContext”
添加完后,View文件夹下多了两个文件,一个是OumindBlog.Context.tt,另一个是OumindBlog.tt。
搭建EF框架需要三个文件和两个组件
A、三个文件:OumindBlog.Context.tt、OumindBlog.tt 和 OumindBlog.edmx
B、两个组件:EntityFrame程序包 和 EF 5.x DbContext Generator for C#
3、编写代码
3.1 在编写代码前,增加一个控制器HomeControllers(HomeController.cs)和一个View(Index.cshtml)
3.2 打开HomeController.cs
3.3 打开Views->Home->Index.cshtml
3.4 运行结果
在解决方案资源管理器中,选择MvcTest,点击“右键”,选中在浏览器中查看或者按快捷键 Ctrl + Shift + W