考虑到使用Entity Framework 5 or up, EF项目构建有两种模式:Db first 和 Code first。顾名思义,Db first就是先设计好数据库框架,然后由VS edmx模型导出相应的数据Models,最终实现各种Business logic services;Code first就是先将所有的Models设计好(各种以来关系),然后通过编译和数据移植的方式产生数据库,同时实现业务层逻辑。
考虑到Db first相对来说简单快捷(设计好了数据库,通过自动化就可以生成Models和Dbcontext entities,缺点是:若修改Models后,重新根据Database生成新的Models的时候,原修改将会被覆盖)。
(1),通过Vistual Studio新建Database项目。
(2),新建各种Tables和存储过程,触发器等等,其中表包括用户表(Users)、分类表(Columns)、博客内容表(Contents)、评论表(Comments)。
一、用户表
CREATE TABLE [dbo].[Users] ( [Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, [UserName] VARCHAR(50) NOT NULL, [Password] VARCHAR(50) NOT NULL, [FullName] VARCHAR(50) NOT NULL, [CreatedUserId] INT, [CreatedTime] DATETIME DEFAULT GETDATE(), [LastUpdatedUserId] INT, [LastUpdatedTime] DATETIME DEFAULT GETDATE(), CONSTRAINT [FK_Users_Users_CreatedUserId] FOREIGN KEY ([CreatedUserId]) REFERENCES [Users]([Id]), CONSTRAINT [FK_Users_Users_LastUpdatedUserId] FOREIGN KEY ([LastUpdatedUserId]) REFERENCES [Users]([Id]) )
二、分类表
CREATE TABLE [dbo].[Columns] ( [Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, [ColumnName] VARCHAR(50) NOT NULL, [Description] VARCHAR(128), [ParentId] INT, [CreatedUserId] INT, [CreatedTime] DATETIME DEFAULT GETDATE(), [LastUpdatedUserId] INT, [LastUpdatedTime] DATETIME DEFAULT GETDATE(), CONSTRAINT [FK_Columns_Users_CreatedUserId] FOREIGN KEY ([CreatedUserId]) REFERENCES [Users]([Id]), CONSTRAINT [FK_Columns_Users_LastUpdatedUserId] FOREIGN KEY ([LastUpdatedUserId]) REFERENCES [Users]([Id]) )
三、博客内容表
CREATE TABLE [dbo].[Contents] ( [Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, [Title] VARCHAR(128) NOT NULL, [Description] VARCHAR(128) NOT NULL, [Article] TEXT NOT NULL, [PublishedUserId] INT NOT NULL, [PublishedTime] DATETIME DEFAULT GETDATE(), [VisitNum] INT DEFAULT 0, [ColumnId] INT NOT NULL, [CommentId] INT, [CreatedUserId] INT, [CreatedTime] DATETIME DEFAULT GETDATE(), [LastUpdatedUserId] INT, [LastUpdatedTime] DATETIME DEFAULT GETDATE(), CONSTRAINT [FK_Contents_Columns_ColumnId] FOREIGN KEY ([ColumnId]) REFERENCES [Columns]([Id]), CONSTRAINT [FK_Contents_Comments_CommentId] FOREIGN KEY ([CommentId]) REFERENCES [Comments]([Id]), CONSTRAINT [FK_Contents_Users_CreatedUserId] FOREIGN KEY ([CreatedUserId]) REFERENCES [Users]([Id]), CONSTRAINT [FK_Contents_Users_LastUpdatedUserId] FOREIGN KEY ([LastUpdatedUserId]) REFERENCES [Users]([Id]) )
四、博客文章评论表
CREATE TABLE [dbo].[Comments] ( [Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, [ContentId] INT NOT NULL, [UserName] VARCHAR(50) NOT NULL, [Email] VARCHAR(50) NOT NULL, [Content] VARCHAR(256) NOT NULL, CONSTRAINT [FK_Comments_Contents_ContentId] FOREIGN KEY ([ContentId]) REFERENCES [Contents]([Id]) )
时间: 2024-10-29 19:08:44