Asp.net MVC + EF + Spring.Net 项目实践(二)
这一篇主要介绍数据库和entity framework集成
1. 先创建数据库,很简单的几张表,但也涉及了一些主外键关系、联合主键等,关系图如下(DB脚本在最后)
2. 打开VS,现在建立Model实体,在Repository层右键,选择“新建项目”,添加“数据”-“ADO.NET 实体数据类型”,名称改为“SMSContext”,也可以后期再修改
3. 点击“添加”选择“来自数据库的 Code First”选项,什么?没有此选项?呵呵~~~(请下载eftool http://www.microsoft.com/en-us/download/details.aspx?id=40762)
4. 点击“下一步”,选择“Microsoft SQL Server”,之后选择数据库
5. 按上面的配置,将App.Config文件先单独存储,后面我们会把连接字符串放到WEB项目中,再把这个文件删掉的
选择要生成Model的表,点击“完成”
6. VS会自动引入EntityFramework和另的一些dll,生成SMSContex.cs还有对应的Grade, Score, Student, Subject类,也就是咱们需要的Model,如下图所示
7. 删除没用的Class1.cs,再创建一个单元测试项目来验证一下(记得要把app.config复制一份放到测试项目里),发现能取出内容,OK。
相关的数据库SQL
USE [SMS] GO /****** Object: Table [dbo].[Grade] Script Date: 2015/3/27 15:28:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Grade]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Grade] 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 /****** Object: Table [dbo].[Score] Script Date: 2015/3/27 15:28:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Score]( [StudentID] [int] NOT NULL, [SubjectID] [int] NOT NULL, [Score] [float] NOT NULL, CONSTRAINT [PK_Score] PRIMARY KEY CLUSTERED ( [StudentID] ASC, [SubjectID] 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].[Student] Script Date: 2015/3/27 15:28:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Student]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, [Age] [int] NULL, [Height] [int] NULL, [Weight] [float] NULL, [Gender] [char](1) NULL, [GradeID] [int] NULL, CONSTRAINT [PK_Student] 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 /****** Object: Table [dbo].[Subject] Script Date: 2015/3/27 15:28:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Subject]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, [Comment] [nvarchar](250) NULL, CONSTRAINT [PK_Subject] 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 IDENTITY_INSERT [dbo].[Grade] ON GO INSERT [dbo].[Grade] ([ID], [Name]) VALUES (1, N'一年级一班') GO INSERT [dbo].[Grade] ([ID], [Name]) VALUES (2, N'一年级二班') GO INSERT [dbo].[Grade] ([ID], [Name]) VALUES (3, N'一年级三班') GO SET IDENTITY_INSERT [dbo].[Grade] OFF GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (1, 1, 80.5) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (1, 2, 90) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (2, 2, 100) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (2, 3, 99.5) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (3, 3, 99) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (3, 4, 98) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (4, 1, 89) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (4, 2, 90) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (4, 3, 91) GO INSERT [dbo].[Score] ([StudentID], [SubjectID], [Score]) VALUES (4, 4, 92) GO SET IDENTITY_INSERT [dbo].[Student] ON GO INSERT [dbo].[Student] ([ID], [Name], [Age], [Height], [Weight], [Gender], [GradeID]) VALUES (1, N'小明', 6, 125, 20, N'M', 1) GO INSERT [dbo].[Student] ([ID], [Name], [Age], [Height], [Weight], [Gender], [GradeID]) VALUES (2, N'小花', 6, 120, 19.5, N'F', 1) GO INSERT [dbo].[Student] ([ID], [Name], [Age], [Height], [Weight], [Gender], [GradeID]) VALUES (3, N'可心儿', 7, 120, 18, N'M', 2) GO INSERT [dbo].[Student] ([ID], [Name], [Age], [Height], [Weight], [Gender], [GradeID]) VALUES (4, N'小灰灰', 7, 130, 19.8, N'M', 3) GO SET IDENTITY_INSERT [dbo].[Student] OFF GO SET IDENTITY_INSERT [dbo].[Subject] ON GO INSERT [dbo].[Subject] ([ID], [Name], [Comment]) VALUES (1, N'数学', NULL) GO INSERT [dbo].[Subject] ([ID], [Name], [Comment]) VALUES (2, N'语文', NULL) GO INSERT [dbo].[Subject] ([ID], [Name], [Comment]) VALUES (3, N'英语', NULL) GO INSERT [dbo].[Subject] ([ID], [Name], [Comment]) VALUES (4, N'政治', NULL) GO SET IDENTITY_INSERT [dbo].[Subject] OFF GO ALTER TABLE [dbo].[Score] ADD CONSTRAINT [DF_Score_Score] DEFAULT ((0)) FOR [Score] GO ALTER TABLE [dbo].[Score] WITH CHECK ADD CONSTRAINT [FK_Score_Student] FOREIGN KEY([StudentID]) REFERENCES [dbo].[Student] ([ID]) GO ALTER TABLE [dbo].[Score] CHECK CONSTRAINT [FK_Score_Student] GO ALTER TABLE [dbo].[Score] WITH CHECK ADD CONSTRAINT [FK_Score_Subject] FOREIGN KEY([StudentID]) REFERENCES [dbo].[Subject] ([ID]) GO ALTER TABLE [dbo].[Score] CHECK CONSTRAINT [FK_Score_Subject] GO ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_Grade] FOREIGN KEY([GradeID]) REFERENCES [dbo].[Grade] ([ID]) GO ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Grade] GO