This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Develop a sample application using LINQprogramming and the ADO.NET Entity Frameworkwith IBM DB2, IDS, and U2 serversRapid application development for DB2, Informix DynamicServer, and U2 using IBM Data Server Provider for .NET andIBM Database Add-ins for Visual Studio
The IBM Data Server Provider for .NET enables application developers to takeadvantage of the Microsoft® Entity Framework and LINQ. You can easily developdata access .NET applications using one of the IBM data servers (DB2®, Informix®Dynamic Server, or U2), the Microsoft ADO.NET Entity Framework, and LINQ. Thisarticle shows you how to get started using the Entity Framework with one of the IBMdata servers, walking you step-by-step through the process of creating your ownapplication.
Introduction
The IBM Data Server Provider for .NET lets you take advantage of the MicrosoftEntity Framework using IBM data servers (DB2, IDS, and U2). You can generateEntity Data Model (EDM) schemas, and you can write and execute EntitySQL andLINQ statements to Entities applications with the supported IBM data server
versions. It enables developers to query and manipulate data using a conceptualmodel instead of a physical storage model.
This article shows you how to get started with LINQ and the Entity Framework usingIBM data server databases. You'll walk step-by-step through the process ofdeveloping a simple Windows® application, generating an EDM model from an IBMdata server database schema, creating LINQ queries that access data, binding thequery results to controls, making updates to objects, and persisting the changes tothe database.
IBM Data Server Provider for .NET gives you many more capabilities, including:
• Mapping details CSDL/SSDL/MSL
• Entity SQL and Query Builder
• Lazy(Deferred) and Eager Loading
• Object services and change tracking
• EDM function import with Store Procedures and U2 subroutines
• Canonical functions
• Optimistic concurrency
• ADO.NET Data Services (RESTful)
• SilverLight and AJAX development
• ASP.NET Dynamic Data
• Visual Studio Tools for Office (VSTO) development using EDM
Many of these capabilities will be discussed in future articles.
Highlighting IBM Entity Framework-enabled ADO.NET Provider
The IBM Data Server Provider for .NET supports the following IBM Data Servers:
• DB2 for Linux®, UNIX®, and Windows, Version 9.1 (or later)
• DB2 for IBM i5/OS® V5R3
• DB2 for IBM i 5.4
• DB2 for z/OS® V7, V8, and V9
• IBM Informix Dynamic Server, Version 11.10 (or later)
• UniVerse 10.2 (or later) and UniData® 7.1 (or later)
The IBM Data Server Provider for .NET enables EDM generation using the followingVisual Studio 2008’s ADO.NET Entity Data Model (see Resources for limitations):
• Tables
• Views
• Stored procedures and U2 subroutines
• LINQ to Entities
• Entity SQL and Entity Provider
• Query Builder
• Object Services
Understanding system requirements
You need the following software to complete the example:
• IBM Data Server Driver for ODBC, CLI, and .NET (version 9.5.3 or later)
• IBM Database Add-ins for Visual Studio (version 9.5.3 or later)
• Visual Studio 2008 Service Pack 1
• .NET Framework 3.5 Service Pack 1
You can access links to download the products from Resources.
Installing
IBM Data Server Provider for .NET is a component of IBM Data Server Driver forODBC, CLI, and .NET. To verify the version:
1. Go to the \netf20 directory (such as c:\Program Files\IBM\IBMDATA SERVER DRIVER\bin\netf20).
Before running the LINQ and Entity tutorial below, check your connection. IBM ClientInstallation contains a utility tool called testconn20.exe. This is shipped withinstallation, and it is usually located in c:\Program Files\IBM\SQLLIB\BIN. Toconfirm Connection Testing, go to a command prompt and run the utility using theappropriate command from Table 1.
Table 1. Testing connectionIBM data server Command Remarks
Use the Entity Data Model wizard to generate an Entity Data Model (EDM) from IBMdata servers.
This article uses the IBM U2 UniVerse ADONET (HS.SALES) sample database forthe example. For other servers, the specific tables and columns are different.However, the steps are the same. (Note that the DB2 iSeries servers have an APARthat affects the operation of the EDM Designer. See Resources for moreinformation.)
To add the ADO.NET Entity Data Model item template, do the following:
1. Select the project in Solution Explorer, point to Add, right-click, and clickNew Item.
2. Select ADO.NET Entity Data Model in the Templates pane
3. Type Customer.edmx for the model name, and click Add.
4. Select the IBM DB2, IDS and U2 Servers (IBM DB2, IDS and U2 DataProvider for .NET Framework) data source in the Choose Data Sourcedialog box.
5. Click Continue.
6. In the Connection Properties dialog box, enter your user name, password,server name, and database name. For example, for DB2 LUW, thedatabase is sample, and for UniVerse, the database is ADONET.
8. For U2 (UniVerse and UniData), select the Advanced button, andindicate the ServerType and Pooling. The Choose Your Data Connectionsdialog box is updated with your database connection settings.
9. Make sure that Save entity connection settings in App.Config as ischecked, and that the value is set to ADONETEntities, as shown inFigure 10.Figure 10. Save Connection String
11. Make sure that all tables and stored procedures are selected and that thevalue of Model namespace is ADONETModel.
12. Click Finish to complete the wizard.
The wizard does the following:
• Adds references to the System.Data.Entity, System.Runtime.Serialization,and System.Security assemblies
• Generates the Customer.edmx file that defines the EDM
• Creates a source code file that contains the classes that were generatedbased on the EDM. You can view the source code file by expanding the.edmx file in the Solution Explorer.
• Creates an App.Config file
To view the EDM in the ADO.NET Entity Data Model Designer, go to the Solution
Explorer and double-click the Customer.edmx file. This displays the model in theADO.NET Entity Data Model Designer window, as shown in Figure 12.
Figure 12. Generated EDM
Note that for U2 data servers, there is a limitation that prevents the EDM Designerfrom discovering associations automatically. See Appendix for a workaround.
Query entities, association, and data binding with controls
To query the database:
1. At the beginning of the code file for the form, add the following code using(C#) statements to refer to the model created from the database and theentity namespace:
C#
using System.Data.Objects;using System.Data.Objects.DataClasses;
2. At the top of the partial class definition for the form, add the following
To save changes made to objects, do the following:
1. In the Toolbox, expand Common Controls, drag the Button control to theform designer, change the name of the control to saveChanges, andchange the Text value to Update.
2. In the form designer, double-click the saveChanges control. This createsthe saveChanges_Click event handler method.
3. Enter the following code to save object changes to the database.
This code disposes of the object context before the form is closed.
To build and run the application, do the following:
1. From the Debug menu, select Start Debugging or Start WithoutDebugging. This builds and starts the application.
2. When the form loads, select a master data from the ComboBox control.This displays the details data that belong to that master data.
3. In the DataGridView, update any information, or add a new item, and clickUpdate. This saves changes to the database and displays a messagebox that declares the number of saved changes, as shown in Figure 13.Figure 13. Run application
Summary
This article provides a good overview of how to use LINQ and Entity Framework withIBM data servers (DB2, IDS and U2). It demonstrated the concept of Entity DataModel and showed the various querying techniques that the Entity Frameworkprovides. It also showed how to add, modify, and delete data using LINQ and EntityFramework.
For U2 Data Servers, a limitation prevents the EDM Designer from discoveringassociations automatically. You can perform this workaround to manually add theassociations.
1. Right-click EDM Designer.
2. Click Add > Association.Figure 14. Establish association
3. Select CUSTOMER and CUSTOMER_ORDERS.Figure 15. Add association
4. On the U2 Data Server, open the Customer.edmx file in an XML editor,and modify @ASSOC_ROW. For example, you can change"@ASSOC_ROW" to ""@ASSOC_ROW""
• Visit the IBM Information Management and Visual Studio resource page ondeveloperWorks to read articles and tutorials and connect to other resources toexpand your .NET skills.
• Refer to the IBM Data Server limitations article in the wiki. Look for the DB2 forIBM i section for information about the DB2 iSeries server APAR that affects theoperation of the EDM Designer.
• Start with overview information from "Access IBM U2 data server from your.NET applications, Part 1: Get an overview of the IBM Data Server for .NET andIBM Database Add-ins" (developerWorks, November 2007).
• Find more from "Create IBM Data Web Services using IBM Database Add-Insfor Visual Studio" (developerWorks, February 2009).
• Work through the tutorial "ASP.NET development with IBM U2 Data Server"(developerWorks, February 2008) to build the next generation application usingUniVerse, UniData, IBM Data Server Provider for .NET, IBM Database Add-ins,and ASP.NET 2.0.
• Learn more about the U2 product family.
• Select the appropriate IBM Data Server client or driver based on this overview.
• Learn more about Information Management at the developerWorks InformationManagement zone. Find technical documentation, how-to articles, education,downloads, product information, and more.
• Stay current with developerWorks technical events and webcasts.
Get products and technologies
• Download the IBM Data Server Driver for ODBC, CLI, and .NET.
• Download IBM Database Add-ins for Visual Studio.
• Build your next development project with IBM trial software, available fordownload directly from developerWorks.
Discuss
• Participate in the discussion forum for .NET development with DB2, IDS, andU2.
• Read the ADO.NET Team blog.
• Check out the developerWorks blogs and get involved in the developerWorkscommunity.
Rajan KumarRajan is a developer with the IBM U2 Data Management Software Group in Denver,Colo. He works on .NET, Windows, and Java data access tools for UniVerse andUniData. He is currently working on the IBM data server provider for .NET and theIBM Database Add-ins for Visual Studio projects.
Brent GrossBrent Gross is a Senior Software Engineer with IBM Information ManagementDevelopment out of the Toronto Lab. He has over 20 years experience with IBM andhas been developing for DB2 since 1995. His responsibilities have includedarchitecture and development of stored procedure support and client interfaces.Brent has presented at many DB2 and user group conferences, His current role is thearchitect of the .NET and OLE DB APIs for DB2 and DB2 Connect.
Trademarks
IBM, DB2, i5/OS, iSeries, Informix, UniData, z/OS, and zSeries are trademarks ofIBM Corporation in the United States, other countries, or both.Microsoft, Windows, Windows NT, and the Windows logo are trademarks of MicrosoftCorporation in the United States, other countries, or both.UNIX is a registered trademark of The Open Group in the United States and othercountries.Linux is a registered trademark of Linus Torvalds in the United States, othercountries, or both.Other company, product, or service names may be trademarks or service marks ofothers.