PDF browse: http://files.cnblogs.com/JimmyZhang/Linq-To-Sql-Part_2-Define-Data-Model-Class.pdf
Source:LINQ to SQL (Part.2-defining our model classes)
Glossary
Data Model: Data Model
Designer: designer
Class Library: Class Library
Client project: client project
Pipeline
Entity class: entity class
Instance: instance
Partial class: Partial Classification
Runtime: Runtime
Execution logic: Execution Logic
Relationship associations: links between links
Delay/lazy: latency/Inert Loading
Prefectched: Pre-obtain
In my blog post on "LINQ to SQL part. 1", I discussed the question "What Is LINQ to SQL" and provided some basic scenarios for using it.
In my first article, I provided some code examples to demonstrate how to use LINQ to SQL to perform some common data operation tasks, including the following aspects:
L how to query Databases
L how to update a row in the database
L how to insert rows in the database and establish relationships with these rows
L how to delete a row in the database
L how to call a stored procedure
L how to obtain data by page on the server
I used a LINQ to SQL class model as shown in to implement all the above operations.
This article is the second article in this series. I will explain in more detail how to create the LINQ to SQL data model shown in.
As part of. Net 3.5, all the features I will discuss in this article will be released along with Visual Studio "orcas.
By downloading Visual Studio "orcas" beta 1 or visual web developer express "orcas" beta1, you can follow the steps below to learn step by step. They can be installed on the same machine as vs2005.
Create a new LINQ to SQL data model
You can click the "Add new project" tab in Visual Studio, and select "LINQ to SQL" to add a data model of LINQ to SQL to an ASP.. Net class library or Windows client project.
When you select "LINQ to SQL", the system automatically runs the LINQ to SQL designer and allows you to use it to create a class that represents the structure of a relational database. It will also create a strongly-typed "datacontext" class. The attributes of this Class represent each table in the database for which we created the model and each stored procedure of the model we created. As in part. 1 as described in that article, the datacontext class is a major pipeline. We use it to query the entities in the database and store changes to the database.
The following is an empty LINQ to SQL ORM (Note: Object/link ing, see part.1, this is what you will see after creating a new LINQ to SQL model.
Entity class
You can create a LINQ ing from a class to a database or from a database to a class by using LINQ to SQL. These classes are generally called "entity classes", and their instances are called "entities ". Ing object classes to tables in the database. The attributes of the object class are usually mapped to the fields in the table. Each entity class instance represents a row in the database table.
Entity classes defined by LINQ to SQLNoInherit a specific base class, which means you can let it inherit from any object you want. All the classes created by using the LINQ to SQL designer are defined as "partial Classification", which means you can have in-depth code of your choice, add some additional attributes, methods, and events to them.
Different from the dataset/tableadapter method provided in vs2005, when using the LINQ to SQL designer, you do not need to provide SQL query statements when defining your data model and data access layer.
Instead, you only focus on defining your entity classes and how they are mapped from the database/to the database and their relationships. The LINQ to QL or/M implementation will create appropriate SQL Execution logic for you at runtime when you interact with or use these entities. You can use the LINQ query syntax to explicitly specify how you want to query your data model in a strongly typed manner.
When you add the two tables (categories and products) and views (invioces) from "northwind" shown above to the LINQ to SQL designer. Based on the underlying database architecture, you will automatically obtain the three entity classes shown below.
Using the data model class defined above, I can now run all the sample code demonstrated in Part 1 (except the stored procedure ). I can query, add, delete, modify, and perform server-side paging operations without writing any additional code or configuration.
Naming and pluralization
When using the LINQ to SQL designer, one thing you should note is that when it automatically adds entity classes for you based on your database architecture, it will automatically create different tables and fields for your "pluralize. For example, the "Products" table in the preceding example produces a "product" class, while the "categories" Table produces a "category" class. This type of naming helps your model and.. Net naming conventions are consistent, and I often find it very convenient to use the editor to do this (especially when adding a large number of tables to your model ).
If you do not like the names of tables or fields generated by the designer, you can modify them and change them to any name you want. You can modify the object/attribute name in the designer or the attribute column.
Note:Pluralization. No proper word translation can be found. according to the context, it can be understood as "standard name ".
The ability to set object/attribute/contact names is different from the names in your database architecture and is useful in many cases, especially:
1. When the name of your background database table/field changes. Because your entity model can have different names than the background architecture, you can decide to update only your ing rules, but not your application or query code, to use the new table/field name.
2. When you have a database architecture, the object name is not very "clean ". For example, use a name such as "au_lname" and "au_fname" as the attribute name of your object class, you can change them to "lastname" and "firstname" in your entity class and develop them based on the name (without changing the field name in the database ).
Relationship
When you drag an object from the server bar to the LINQ to SQL designer, Visual Studio checks the primary key/foreign key relationship of the object, based on this relationship, a default "link between links" is automatically created between different object classes it creates ". For example, when I add the products and categories tables to my LINQ to SQL designer from northwind, you can see that a one-to-many relationship is established between them (this is indicated by an arrow in the designer ).
The preceding Association will enable the product entity class to have a "category" attribute. With this attribute, developers can access the category of a given product. It also enables the category class to have a "Products" set through which developers can obtain all the products of a given category.
If you do not like the method of designer modeling or naming, You can overwrite it. Click the arrow indicating the link in design, find its properties in the property bar, and rename, delete, or modify it.
Latency/inertia loading
LINQ to SQL allows developers to specify whether the attributes in an object should be pre-obtained or delayed/Inert during the first access. You can select any object attribute or link in the designer to define default pre-acquisition/delayed loading rules for object attributes, set the "delay loaded" attribute in the attribute column to true or false.
Let's give an example to illustrate when we need to do this. Consider the "category" entity class we just modeled. "Northwind" has a "categories" table, which contains a "picture" field, which contains (probably very large) binary data of a type, when I use it, I only want to obtain binary data in the database (instead of listing the category name in a query ).
You can configure it by selecting it in the designer and setting the late Loading Value of picture in the property bar.
Use stored procedures
LINQ to SQL allows you to model a stored procedure as a method in the datacontext class. For example, we define a simple stored procedure as follows to obtain the product information based on the categoryid:
I can use the server panel in Visual Studio to drag and drop the stored procedure to the LINQ to SQL designer to add a strongly typed method, which is used to call this stored procedure. If I drag a stored procedure to the designer's "product" entity, the LINQ to SQL designer declares that the stored procedure returns an ienumerable <product>:
Then, I can use the LINQ query syntax (which will generate an actual SQL query statement), or I can call the Stored Procedure added above to obtain the product from the database.
Update, delete, and insert data using Stored Procedures
By default, When you insert, update, or delete an object, the appropriate SQL expression is automatically created for the LINQ to SQL statement. For example, you can write the following LINQ to SQL code to update some values of the "product" instance.
By default, when you submit changes, the appropriate "Update" statement will be created for you (I will discuss it in detail later ).
You can also define and use custom insert, update, and delete stored procedures. Set these parameters, click the entity class in the LINQ to SQL designer, and click "…" in the delete/update/Insert attribute in its attribute column. Select a specific pre-written stored procedure.
The advantage of changing the above settings is that it is only performed on the LINQ ing layer of the LINQ to SQL, which means that the update code I demonstrated earlier can continue to be used without any modifications. This avoids developers from having to rewrite the Code even if they later use a custom stored procedure.
Summary
LINQ to SQL provides a good and refreshing way to model the data layer in your application. Once you define your data model, you can use it to query, insert, update, and delete data easily and efficiently.
By using Visual Studio and the built-in LINQ to SQL designer of Visual Web Developer Express, you can quickly create and manage your LINQ to SQL data model. The LINQ to SQL designer also provides great flexibility, allowing you to customize default behaviors and overwrite/extend the system to meet your special needs.
In the next article, I will use the data model we just created to thoroughly explore the features of query, insertion, update, and deletion. In the update, insert, and delete essays, I will discuss how to add custom business/data layer validation to our designed entities to provide additional verification logic.