PDF browse: http://files.cnblogs.com/JimmyZhang/Linq-To-Sql-Part_1-Introduction.pdf
Source:LINQ to SQL (part.1-Introduction)
Glossary:
Extension: Extended
Initializer: initializer
Expression: Expression
Features: Features
Querying programming model: Query Programming Model
. Net Language Integrated Query:. Net integrated language query
Type-safty: type security
Compile-time: compilation phase
Extensibility model: scalable model
Domain-specific: Specific Domain Name
Object Relational ing: object relationship ing
Designer: designer
Representation: Performance
Entity class: entity class
Association/relationship: contact/relationship
Solution: Solution
Pipeline
In the past few months, I wrote a series of blogs about the coming Visual Studio and. NET Framework "orcas" versions. Below are the links of these essays.
L automated attributes, objects, set starters
L Extension Method
L Lambada expression
L query syntax
L anonymous type
These language features help to establish a programming concept for the use of data queries. We call this query Programming Model "LINQ"-to represent the. NET Integrated Query Language.
Developers can use LINQ in any data source. They can experience an efficient query in their programming Fables. They can select to convert/assemble the data query results to any format they want, and, these query results can be easily operated. The language that supports LINQ provides comprehensive support for type security and Query expressions detected during compilation. In addition, the development tool can provide completely intelligent prompts when writing the LINQ code (note: that is, you enter an I, and the int comes out directly. You only need to take a space .) Debugging and rich refactoring support.
LINQ supports a rich set of extensible models, which makes it very convenient to create efficient operations for specific domain names for data sources.
The. NET Framework "orcas" version (Note: Now renamed. NET Framework 3.5 and vs2008) provides built-in libraries to support objects, XML, and various databases.
What is LINQ to SQL?
LINQ to SQL is included in. an O/RM (Object relationship ing) in the "orcas" version of Net Framework, and O/RM allows you to use.. Net class. Then, you can use LINQ to query, update, add, and delete data in the database.
LINQ to SQL provides full support for transactions, views, and stored procedures. It also provides a simple implementation method for integrating the data validation and business layer logic into your data model.
Use LINQ to SQL to model databases
Visual Studio "orcas" provides a LINQ to SQL designer to provide a simple and Visualized Method for modeling databases to objects. This designer will be used more deeply in my next blog article (You can also download the video I recorded in March, this video records how I created a LINQ to SQL model ).
Using the LINQ to SQL designer, I can easily establish the performance of the Sample Database "northwind. :
As shown in, my LINQ to SQL design comes from four entity classes: product, category, order, and orderdetail. The attributes of each class are mapped to the fields of the corresponding database table. An instance of each class represents a row of a table in the database.
The arrows between the above four object classes represent the links/relationships between different objects. The direction of the arrow indicates whether the relationship is one-to-many or multiple-to-one. Strongly typed attributes are added to the object class based on this. For example, the category class and the product class have a one-to-multiple relationship. This means that the category class will have a "categories" attribute, which is a collection of all product objects belonging to this category. The product class will have a "category" attribute, which points to the category class it belongs.
The rectangular block on the right of the LINQ to SQL designer contains a list of stored procedures that interact with our database model. In this example, I added a simple "getproductsbycategory" stored procedure. It uses categoryid as the input parameter and returns a collection of product instances. Next let's take a look at how to call this stored procedure in code.
Understanding datacontext
When you press the "save" button in the LINQ to SQL designer, Visual Studio maintains the classes that we create models and represent the relationship between entities and databases. Each LINQ to SQL designer file will be added to our solution, and a custom datacontext class will also be created. This datacontext class is a major pipeline through which we can operate and query databases. The datacontext class will contain some attributes, which represent the table we created during modeling and the stored procedure we added.
For example, the following is a northwinddatacontext class, which strictly follows the above design.
Code demonstration of LINQ to SQL
Once we use the LINQ to SQL designer to create a model for our database, we can easily write code to access it. The following is a sample code that demonstrates some basic tasks.
1) query the product from the database
The following code uses the LINQ query syntax to obtain a series of product objects. Note how the code is queried through the product/Category multi-to-one relationship, and only filter out the products that belong to the "beverage" category.
2) update a product in the database
The following code demonstrates how to get a product from the database, update its price, and save it back to the database.
3) Insert a new category and two new products into the database.
The following code demonstrates how to create a new category, create two new products, and establish the relationship between them. Finally, they are all stored back to the database.
Note that you do not need to manually set the primary key/foreign key relationship below. Instead, simply add a product object to the "Products" set of category, and then add this category object to the "categories" set of datacontext, as soon as you know, you need to automatically maintain the appropriate primary key/external relationship for me.
4) Delete products from the database
The following code demonstrates how to delete all toy products in the database.
5) call a stored procedure
The following code demonstrates how to obtain the product entity by calling the previously added "getproductsbycategory" stored procedure, rather than using the LINQ query syntax. Note that once I obtain the products result set, I can update and delete them, and then call dB. submitchanges () to upload these changes back to the database.
6) obtain products by page on the server
The following code demonstrates how to implement efficient data paging on the server as part of the LINQ query. By using the following SKIP () and take () operations, we will only return 10 rows-the initial 200 rows from the database.
Summary
LINQ to SQL provides a good and refreshing method for modeling the data layer in your application. Once you define your data model, you can query, insert, update, and delete it in a simple and efficient manner.
We hope that the above introduction and code examples will help you meet your desire to learn more. In the next few weeks, I will continue to post this series of articles to further explore the scope of the article.