点餐网站学习(EF+MVC)-EF工具,自动创建模型并建立数据库映射关系

  引言:在创业公司,很多情况下需求都不是很明白,因为在初衷都只是想法,没有一个具体的东西。为了成本考虑我们如果同步开发昵!我们得想一想是不是只有设计图完全出来后我们才能做相关的开发昵!结果不然,我们只需要知道大概要做什么功能,如何去分析功能昵,我的想法是OOP思想,针对对象去分析,以对象为单位来做功能也就是说我们的每一个功能(增,删,查,改)都以对象去做,我是以尚餐网之前的功能分析去参考如下:

1.分析数据结构创建数据库

  

创建权限中的角色表

CREATE TABLE [dbo].[tbl_Role](
[RoleID] [int] IDENTITY(1,1) NOT NULL,
[RoleName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_tbl_Role] PRIMARY KEY CLUSTERED
(
[RoleID] 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'tbl_Role', @level2type=N'COLUMN',@level2name=N'RoleID'
GO
/****** Object: Table [dbo].[tbl_Membership] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的成员
CREATE TABLE [dbo].[tbl_Membership](
[MembershipID] [int] IDENTITY(1,1) NOT NULL,
[MembershipAccount] [nvarchar](50) NOT NULL,
[MemberShipPassword] [nvarchar](50) NOT NULL,
[MemberShipState] [int] NOT NULL,
[MemberShipCreate] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_Membership] PRIMARY KEY CLUSTERED
(
[MembershipID] 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'tbl_Membership', @level2type=N'COLUMN',@level2name=N'MembershipID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员账号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Membership', @level2type=N'COLUMN',@level2name=N'MembershipAccount'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员密码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Membership', @level2type=N'COLUMN',@level2name=N'MemberShipPassword'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员状态' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Membership', @level2type=N'COLUMN',@level2name=N'MemberShipState'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Membership', @level2type=N'COLUMN',@level2name=N'MemberShipCreate'
GO
/****** Object: Table [dbo].[tbl_ShopInfo] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建商家信息
CREATE TABLE [dbo].[tbl_ShopInfo](
[ShopInfoID] [int] IDENTITY(1,1) NOT NULL,
[MembershipID] [int] NOT NULL,
[ShopInfoName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_ShopInfo] PRIMARY KEY CLUSTERED
(
[ShopInfoID] 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'tbl_ShopInfo', @level2type=N'COLUMN',@level2name=N'ShopInfoID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_ShopInfo', @level2type=N'COLUMN',@level2name=N'MembershipID'
GO
/****** Object: Table [dbo].[tbl_UserInfo] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建用户信息
CREATE TABLE [dbo].[tbl_UserInfo](
[UserInfoID] [int] IDENTITY(1,1) NOT NULL,
[MembershipID] [int] NOT NULL,
[UserInfoName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_UserInfo] PRIMARY KEY CLUSTERED
(
[UserInfoID] 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'tbl_UserInfo', @level2type=N'COLUMN',@level2name=N'UserInfoID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_UserInfo', @level2type=N'COLUMN',@level2name=N'MembershipID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户信息名字' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_UserInfo', @level2type=N'COLUMN',@level2name=N'UserInfoName'
GO
/****** Object: Table [dbo].[tbl_MembershipInRole] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的成员与角色
CREATE TABLE [dbo].[tbl_MembershipInRole](
[RoleID] [int] NOT NULL,
[MembershipID] [int] NOT NULL
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_MembershipInRole', @level2type=N'COLUMN',@level2name=N'RoleID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'成员编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_MembershipInRole', @level2type=N'COLUMN',@level2name=N'MembershipID'
GO
/****** Object: Table [dbo].[tbl_Validation] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_Validation](
[ValidationID] [int] IDENTITY(1,1) NOT NULL,
[ValidationName] [nvarchar](50) NOT NULL,
[ValidationType] [int] NOT NULL,
[ShopInfoID] [int] NULL,
[UserInfoID] [int] NULL
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'验证编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Validation', @level2type=N'COLUMN',@level2name=N'ValidationID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'验证名字' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Validation', @level2type=N'COLUMN',@level2name=N'ValidationName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'验证类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Validation', @level2type=N'COLUMN',@level2name=N'ValidationType'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'商家编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Validation', @level2type=N'COLUMN',@level2name=N'ShopInfoID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Validation', @level2type=N'COLUMN',@level2name=N'UserInfoID'
GO
/****** Object: Table [dbo].[tbl_ShopSet] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建商家设置
CREATE TABLE [dbo].[tbl_ShopSet](
[ShopSetID] [int] IDENTITY(1,1) NOT NULL,
[ShopInfoID] [int] NOT NULL,
[ShopSetName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_ShopSet] PRIMARY KEY CLUSTERED
(
[ShopSetID] 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'tbl_ShopSet', @level2type=N'COLUMN',@level2name=N'ShopSetID'
GO
/****** Object: Table [dbo].[tbl_ShopPhoto] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建商家相册
CREATE TABLE [dbo].[tbl_ShopPhoto](
[ShopPhotoID] [int] IDENTITY(1,1) NOT NULL,
[ShopInfoID] [int] NOT NULL,
[ShopPhotoName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_ShopPhoto] PRIMARY KEY CLUSTERED
(
[ShopPhotoID] 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'tbl_ShopPhoto', @level2type=N'COLUMN',@level2name=N'ShopPhotoID'
GO
/****** Object: Table [dbo].[tbl_Comment] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的评论
CREATE TABLE [dbo].[tbl_Comment](
[CommentID] [int] IDENTITY(1,1) NOT NULL,
[CommentTitle] [nvarchar](100) NOT NULL,
[ShopInfoID] [int] NOT NULL,
[UserInfoID] [int] NULL,
CONSTRAINT [PK_tbl_Comment] PRIMARY KEY CLUSTERED
(
[CommentID] 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'tbl_Comment', @level2type=N'COLUMN',@level2name=N'CommentID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'评论标题' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Comment', @level2type=N'COLUMN',@level2name=N'CommentTitle'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'商家编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Comment', @level2type=N'COLUMN',@level2name=N'ShopInfoID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_Comment', @level2type=N'COLUMN',@level2name=N'UserInfoID'
GO
/****** Object: Table [dbo].[tbl_Order] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的订单
CREATE TABLE [dbo].[tbl_Order](
[OrderID] [int] NOT NULL,
[OrderNo] [nvarchar](100) NOT NULL,
[ShopInfoID] [int] NOT NULL,
[UserInfoID] [int] NULL,
[OrderPeople] [nvarchar](50) NULL,
[OrderTelephone] [nvarchar](30) NULL,
CONSTRAINT [PK_tbl_Order] PRIMARY KEY CLUSTERED
(
[OrderID] 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
/****** Object: Table [dbo].[tbl_ShopDishType] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的菜品分类
CREATE TABLE [dbo].[tbl_ShopDishType](
[ShopDishTypeID] [int] IDENTITY(1,1) NOT NULL,
[ShopInfoID] [int] NOT NULL,
[ShopDishTypeName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_tbl_ShopDishType] PRIMARY KEY CLUSTERED
(
[ShopDishTypeID] 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'tbl_ShopDishType', @level2type=N'COLUMN',@level2name=N'ShopDishTypeID'
GO
/****** Object: Table [dbo].[tbl_ShopDishInfo] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建权限中的菜品
CREATE TABLE [dbo].[tbl_ShopDishInfo](
[ShopDishInfoID] [int] IDENTITY(1,1) NOT NULL,
[ShopDishTypeID] [int] NOT NULL,
[ShopDishInfoName] [nvarchar](100) NULL,
CONSTRAINT [PK_tbl_ShopDishInfo] PRIMARY KEY CLUSTERED
(
[ShopDishInfoID] 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'tbl_ShopDishInfo', @level2type=N'COLUMN',@level2name=N'ShopDishInfoID'
GO
/****** Object: Table [dbo].[tbl_OrderDetails] Script Date: 02/22/2013 09:31:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

创建订单详情
CREATE TABLE [dbo].[tbl_OrderDetails](
[OrderDetailsID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NOT NULL,
[ShopDishInfoID] [int] NOT NULL,
CONSTRAINT [PK_tbl_OrderDetails] PRIMARY KEY CLUSTERED
(
[OrderDetailsID] 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'tbl_OrderDetails', @level2type=N'COLUMN',@level2name=N'OrderDetailsID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'商家具体菜编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_OrderDetails', @level2type=N'COLUMN',@level2name=N'ShopDishInfoID'
GO

大家可以看到,我只是把数据结构及关系做了!有人一定会问哪以后数据表中的字段一变,程序的模型不是也要变吗?哪不是很麻烦嘛!

二.EF Power Tool创建模型直接映射数据库

  之前的文章我已经介绍过EF Power Too哪我在说说吧!

  安装Entity Framework Power Tools Beta 3特别说一下这个小工具的功能:

  1) 按照现有数据库结构,生成Code First POCO class、DbContext class和相应的mapping class。

  2) 以designer模式或XML模式查看POCO class对应的Entity Data Model (edmx) 。

  3) 查看Entity Data Model所对应的DDL。

  4) 生成EF Generated View提高EF性能。

  具体步骤如下:

  1) 创建项目

  

  2)使用EF Power Tool

  

  3)EF工具关联数据库,形成映射关系

  

  4)结果

  

posted @ 2013-02-22 10:08  Net_倒计时  阅读(1413)  评论(4编辑  收藏  举报