Using DLINQ with ASP.NET (Part 2 of my LINQ series)
page 4 of 10
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 38677/ 60

Step 2: Create a DLINQ enabled object model for the Northwind database

We’ll use the “Northwind” database sample that comes with SQL Server for our LINQ/DLINQ code below. 

We’ll start by creating an object model that maps to the Northwind database.  We can do this one of three ways:

1)     Write this by hand

2)     Use the new VS DLINQ designer to graphically define the class organization and relationships

3)     Use the built-in “SQLMetal.exe” command-line utility to create it for us

For this sample walkthrough I’m going to use #3.  To create the mapping I simply need to type the below statements in a command-prompt to create this for the Northwind database on my local box:

Listing 4

>> cd c:\Program Files\LINQ Preview\Bin
>> sqlmetal /database:Northwind /pluralize /namespace:Northwind /code:Northwind.cs

SqlMetal will infer the appropriate classes from the database metadata, and by default add appropriate validation constraint checks (NOT NULL, Column Types and Size Limits, etc) to the object model.  Because I’ve specified the /pluralize parameter option, it will use some default naming logic conventions to create the property and table names – for example creating a “Customer” class that maps to the “Customers” table (obviously you can go in and override these if you don’t like the defaults). 

We could optionally also specify a /map:[filename] parameter to control whether the database mappings are stored using code attributes or in an XML metadata file.  We could also indicate that stored procedures, functions and views should be mapped via the /views, /sprocs, /functions parameter switches.

The result of the SQLMetal’s code generation will be saved within the “Northwind.cs” file we specified and scoped within the “Northwind” code namespace above.  We’ll then want to copy this file under the “/app_code” directory within our new ASP.NET project:

Figure 2

The last step we’ll then want to do is to add a “<connectionStrings>” section within the web.config of our application to configure our database connection information:

Listing 5

    <add name="Northwind" 
         connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True" 

This will allow us to easily change the database deployment location later.  With ASP.NET 2.0 we can also now optionally encrypt the web.config values for secure deployment.  ASP.NET 2.0 also installs a built-in admin extension to the IIS admin tool that allows IT administrators easily manage the connection strings during and after deployment as well.

Once the above steps are done, we are ready to use our newly created Northwind DLINQ data layer anywhere within our application.

View Entire Article

User Comments

Title: DLINQ Usage   
Name: Subhashini
Date: 2010-12-18 1:36:06 AM
This article solved many of questions about DLINQ.I understand how to do create database structures for DLINQ query using Attribute Based Mapping and through XML Based Mapping.
Title: Helping hand for the DLINQ users   
Name: Tejaswini Jangale-Chaudhari
Date: 2009-04-22 1:02:16 AM
The article is real nice, n helped me a lot to understand binding and pagination. Keep posting such helpful articles :)
Title: Great Article   
Name: Yuna
Date: 2008-07-18 3:42:52 AM
thank you very much, my article help me understand LINQ
Title: Great Article   
Name: Jaykumar Acharya
Date: 2008-06-18 9:33:59 AM
I must say you are my guru of LINQ. These posts really helped me to learn LINQ and DLINQ. It was like a cake walk to learn deep concepts. Please keep posting such blogs for us.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-23 12:47:06 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search