【IT168 技术文档】
准备工作
预计完成本实验所需的时间
40 分钟
目标
在完成本实验后,您将可以:
• 创建数据库的实体数据模型
• 使用LINQ查询语言访问实体数据模型
先决条件
在完成本实验前,您必须具有:
• 使用C#编写ADO.NET数据访问技术的相关经验。
实验场景
A trend among database developers is to define high-level business objects, or entities, that they then map to the tables and columns stored in a database. Rather than programming against tables and columns in a database, developers use high-level entities such as ‘Customer’ or ‘Order’ to represent the underlying data. The ADO.NET Entity Framework enables developers to program against relational data in terms of such entities. Programming at this level of abstraction is highly productive and allows developers to take full advantage of entity-relationship modeling.
The object services layer of ADO.NET enables the materialization, change tracking, and persistence of data as Common Language Runtime (CLR) objects. Developers using the ADO.NET Entity Framework can program against a database by using CLR objects that are managed by ADO.NET. SQL Server 2008 introduces more efficient, optimized support that improves performance and simplifies development.
Microsoft Language Integrated Query (LINQ) enables developers to issue queries against data by using a managed programming language such as C# or Visual Basic.NET, instead of SQL statements. LINQ enables seamless, strongly typed, set-oriented queries written in .NET Framework languages to run against ADO.NET (LINQ to SQL), ADO.NET DataSets (LINQ to DataSets), the ADO.NET Entity Framework (LINQ to Entities), and to the Entity Data Service Mapping Provider. SQL Server 2008 features a new LINQ to SQL Provider that enables developers to use LINQ directly on SQL Server 2008 tables and columns.
虚拟机环境
1. 从开始菜单或桌面上启用Microsoft Virtual PC 。如果Virtual PC 控制台没有启用,请查看系统托盘,然后双击系统托盘当中的Microsoft Virtual PC 。
2. 选择Sql08 然后点击Start。
3. 在虚拟机运行起来后,可以通过点击右Alt+Del 来向虚拟机发送一个Ctrl+Alt+Del 命令。
4. 在登录窗口中,输入以下信息:
• User name: administrator
• Password: password01!
练习 1:创建实例数据模型
创建Web应用程序
1. 点击Start | All Programs | Microsoft Visual Studio 2008 | Microsoft Visual Studio 2008,启动Microsoft Visual Studio 2008集成开发环境。
2. 在菜单中,点击 File | New | Web Site 来打开 New Web Site对话框。
3. 然后,选择 ASP.NET Web Site 模板,在 Location中选择File System,然后在Language下拉列表中选择Visual C#,点击OK,创建一个新的网站。
添加实体数据模型
1. 查看Solution Explorer,在新建的项目中,右键单击网站节点,选择Add New Item菜单项,打开Add New Item对话框。
2. 在Add New Item对话框中,选择ADO.NET Entity Data Model作为模板,并在New文本框中输入Northwind.edmx,然后在Language下拉列表中选择Visual C#,然后点击OK,添加实体数据模型。
3. 如果弹出新的对话框,询问是否将代码旋转在App_Code目录当中,选择Yes。
4. 这时,将弹出Entity Data Model Wizard对话框。
5. 选择Generate from database,点击Next。
6. 接下来,在Choose your data connection下拉列表中,选择Northwind数据连接,如果没有这个连接,则点击”New Connection …”以创建一个到Northwind数据库的连接。
7. 在下面的“Save entity connection settings in Web.config as”文本框中,输入NorthwindEntities。
8. 注意,在中间的文本框中,会显示这个实体的连接字符串。
9. 点击Next。
10. 然后,将会看到Choose your database objects。
11. 在下面的树状选择框中,展开Tables节点,选择Customers和Orders两个数据表。
12. 在下面的Model Namespace文本框中,输入Northwind,点击Finish,关闭对话框。
编辑实体数据模型
1. 这时,将打开Northwind.edmx文件的编辑器,可以看到Customers和Orders两个实体。
2. 选中Customers实体,在属性窗口中,将它的Entity Set Name置为Customers,将Name置为Customer。
3. 同样,将Orders实体的Entity Set Name置为Orders,将Name置为Order。
4. 这样,两个实体的名称将被修改为Customer和Order。
5. 并且,您可以看到在两个实体之间,存在着一个一对多的关系。
查看生成代码
1. 在Solution Explorer面板中,在App_Code目录当中,展开Northwind.edmx节点,看到Northwind.edmx.cs文件。右键单击该文件,选择Open以打开代码文件。
2. 在代码编辑器中,可以看到由图形化编辑器所生成的三个类,包括:
i. NorthwindEntities
ii. Customer
iii. Order
3. 其中,Customer和Order就是在图形化编辑器中所定义的两个实体的名称。
4. 而NorthwindEntities是访问底层数据源的实体集的集合,注意到,在NorthwindEntities中拥有两个实体集,名称分别为Customers和Orders,这两个实体集便是在图形化编辑器下所设定的两个实体的Entity Set Name。
编写LINQ to Entity代码
1. 在Solution Explorer面板中,双击Default.aspx文件,在打开的页面编辑器中,点击左下角的Design以切换到设计界面。
2. 查看Toolbox面板,展开Data,将里面的GridView控件拖放到页面当中。
3. 在Solution Explorer面板中,右键单击Default.aspx文件,选择View Code以查看C#源代码。
4. 在代码上方,添加一个命名空间的using:
using Northwind;
5. 在Page_Load方法体中,输入下面的代码进行实体数据模型的访问,并将数据绑定到前面的GridView控件上:
var ordersOf1996 = from order in entities.Orders
where order.OrderDate < new DateTime(1997, 1, 1)
select new
{
order.OrderID,
order.OrderDate,
order.ShipCity,
order.Customers.CompanyName
};
this.GridView1.DataSource = ordersOf1996.Take(20);
this.GridView1.DataBind();
6. 点击键盘上的F5键,以运行应用程序。
7. 在弹出的对话框中,点击OK,如果还有对话框弹出,点击Yes。
8. 这时,便可以看到对于实体数据模型的查询结果。
9. 关闭Visual Studio 2008。
10. 关闭Virtual PC,不保存任何更改。