Mvc项目实例 MvcMusicStore 一
Mvc项目实例 MvcMusicStore 二
Mvc项目实例 MvcMusicStore 三
Mvc项目实例 MvcMusicStore 四
Mvc项目实例 MvcMusicStore 五
第三节 链接数据库
在此之前,我们都是通过url来传递参数,或都通过viewModel类向View视图传递值。在这节里,
我们将慢慢介绍如果通过.net EntityFramework连接和更新数据库
首先我们创建数据库,此示例中,我采用的是sqlserver2005 ;具体的创建数据库的sql语句为:
Create Database MvcMusicStore
use MvcMusicStore
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Artist]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Artist](
[ArtistId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Artist] PRIMARY KEY CLUSTERED
(
[ArtistId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Order]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Order](
[OrderId] [int] IDENTITY(1,1) NOT NULL,
[OrderDate] [datetime] NULL,
[Username] [varchar](50) NULL,
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[Address] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar](50) NULL,
[PostalCode] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[Phone] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[Total] [decimal](18, 0) NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
(
[OrderId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Genre]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Genre](
[GenreId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Description] [varchar](50) NULL,
CONSTRAINT [PK_Genre] PRIMARY KEY CLUSTERED
(
[GenreId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Album]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Album](
[AlbumId] [int] IDENTITY(1,1) NOT NULL,
[GenreId] [int] NULL,
[ArtistId] [int] NULL,
[Title] [varchar](50) NULL,
[Price] [decimal](18, 0) NULL,
[AlbumArtUrl] [varchar](50) NULL,
CONSTRAINT [PK_Album] PRIMARY KEY CLUSTERED
(
[AlbumId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cart]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Cart](
[RecordId] [int] IDENTITY(1,1) NOT NULL,
[CartId] [varchar](50) NULL,
[AlbumId] [int] NULL,
[Count] [int] NULL,
[DateCreated] [datetime] NULL,
CONSTRAINT [PK_Cart] PRIMARY KEY CLUSTERED
(
[RecordId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OrderDetail]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[OrderDetail](
[OrderDetailId] [int] IDENTITY(1,1) NOT NULL,
[OrderId] [int] NULL,
[AlbumId] [int] NOT NULL,
[Quantity] [nchar](10) NULL,
[UnitPrice] [decimal](18, 0) NULL,
CONSTRAINT [PK_OrderDetail_1] PRIMARY KEY CLUSTERED
(
[OrderDetailId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Album_Artist]')
AND parent_object_id =OBJECT_ID(N'[dbo].[Album]'))
ALTER TABLE [dbo].[Album] WITH CHECK ADD CONSTRAINT [FK_Album_Artist] FOREIGN KEY([ArtistId])
REFERENCES [dbo].[Artist] ([ArtistId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Album_Genre]')
AND parent_object_id =OBJECT_ID(N'[dbo].[Album]'))
ALTER TABLE [dbo].[Album] WITH CHECK ADD CONSTRAINT [FK_Album_Genre] FOREIGN KEY([GenreId])
REFERENCES [dbo].[Genre] ([GenreId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Cart_Album]')
AND parent_object_id =OBJECT_ID(N'[dbo].[Cart]'))
ALTER TABLE [dbo].[Cart] WITH CHECK ADD CONSTRAINT [FK_Cart_Album] FOREIGN KEY([AlbumId])
REFERENCES [dbo].[Album] ([AlbumId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OrderDetail_Album]')
AND parent_object_id =OBJECT_ID(N'[dbo].[OrderDetail]'))
ALTER TABLE [dbo].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Album] FOREIGN KEY([AlbumId])
REFERENCES [dbo].[Album] ([AlbumId])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_OrderDetail_Order]')
AND parent_object_id =OBJECT_ID(N'[dbo].[OrderDetail]'))
ALTER TABLE [dbo].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Order] FOREIGN KEY([OrderId])
REFERENCES [dbo].[Order] ([OrderId])
--添加基本数据
Insert into Genre ([Name]) values ('Rock')
Insert into Genre ([Name]) values ('Jazz')
Insert into Genre ([Name]) values ('Metal')
Insert into Genre ([Name]) values ('Alternative')
Insert into Genre ([Name]) values ('Disco')
Insert into Genre ([Name]) values ('Blues')
Insert into Genre ([Name]) values ('Latin')
Insert into Genre ([Name]) values ('Reggae')
Insert into Genre ([Name]) values ('Pop')
Insert into Genre ([Name]) values ('Classical')
Insert into Artist([Name]) values('AC/DC')
Insert into Artist([Name]) values('Lenny Kravitz')
Insert into Artist([Name]) values('Men At Work')
Insert into Album (GenreId,ArtistId,Title,Price,AlbumArtUrl)
values(5,1,'The Best of Billy Cobham',10,'/Content/images/placeholder.gif')
Insert into Album (GenreId,ArtistId,Title,Price,AlbumArtUrl)
values(3,3,'Quiet Songs',10,'/Content/images/placeholder.gif')
Insert into Album (GenreId,ArtistId,Title,Price,AlbumArtUrl)
values(2,2,'Worlds',10,'/Content/images/placeholder.gif')
Insert into Album (GenreId,ArtistId,Title,Price,AlbumArtUrl)
values(2,2,'Special',10,'/Content/images/placeholder.gif')
-首先把上面的sql代码粘贴到sql查询分析器里,然后运行。我们将在sqlserver中创建数据库MvcMusicStore
在vs2010中找到服务器资源管理器à选中数据库链接à添加连接à选择sqlserver服务器à添加链接
添加数据库链接后,显示数据库表如下
至此,我们创建数据完成。下面,我需要做的就是对数据库的读写。
这里我们采用.net EntiryFramework 它是.net提供的一个OR/M框架;
选中Models文件夹,然后右键à添加新建项à找到Ado.net 实体数据模型à修改名称为Store.edmx然后点击添加
进入实体数据模型向导
选择从数据库生成 à下一步
接着下一步
“在模型中加入外键列” 前面打钩 然后点击完成
为了类名好记,我们选中 Store.Designer.cs 找到 public partial class MvcMusicStoreEntities :
重构其名称为: MusicStoreEntities 然后点击确定à应用
从数据库表中查询数据
在资源管理器中找到Models文件夹,然后删除先前创建的album类和Genre类 ; 然后打开StoreContoller
引入命名空间
using MvcMusicStore;
在类中添加MusicStoreEntities实体变量
MusicStoreEntities storeDB = new MusicStoreEntities();
修改Index方法如下:
// GET: /Store/
public ActionResult Index()
{
var genres = (from genre in storeDB.Genre select genre.Name).ToList();
var viewModel=new StoreIndexViewModel{
NumberOfGenres=genres.Count,
Genres=genres
};
return View(viewModel);
}
再次运行项目 浏览/store/ 会看到,已经显示数据库信息了
总结:
这节里主要讲的是如果创建ADO.NET数据实体模型,然后如何通过数据模型查询数据。
在下一节里,我将介绍更复杂的数据操作