Use of the LINQ designer in Visual Studio "orcas"

Source: Internet
Author: User
Tags sql server express

According to the translation of LINQ to SQL (Part 2-defining our data model classes. This version does not support the link of SQL Server 2005 Developer Edition. Therefore, it is integrated with the adventureworks database of SQL Server express edition, to share the usage of the LINQ designer.

1. Create a LINQ Data Model for SQL

You can open the "Add new item" option in Visual Studio to add the data model for SQL to ASP. NET, class library, or Windows applications by selecting the "LINQ to SQL" option.ProgramProject.

When you add the "LINQ to SQL" file to the project, Visual Studio will go to the LINQ designer interface, and now you can perform modeling operations. At the same time, the designer will create a strongly-typed "datacontext" class. The attributes in this class represent every table created from the database to the LINQ, the method represents every stored procedure we select for modeling. The datacontext class is the main channel for querying the entity set from the database and returning changes from the application to the database.

As shown in, this is the orm design interface of LINQ:

Entity class:

LINQ ing database classes can be modeled by SQL-based LINQ. These classes are usually called "entity classes (entity class)", and their instances are called "entities (entity )". The object class maps tables in the database. The attributes in the class usually map fields in the table. each instance of the object class represents a record of the table in the database. The object class defined through LINQ does not inherit any base class, which means you can make it inherit from any object. In addition, classes created using the LINQ designer are defined as "partial classes (local classes)", so you can selectively add classes.Code, Attaches attributes, methods, and events.

Unlike the dataset/tableadapter function provided by Visual Studio 2005, you do not need to specify an SQL query statement when using the LINQ designer to create a data model and access layer. Correspondingly, we should focus on the definition of entity classes and how to map the relationship between databases and them. The implementation of LINQ or/m for SQL is concerned with generating appropriate SQL Execution logic when using data entities at runtime. Use the LINQ query syntax to express how to query data models in a strongly typed manner.

2. Create an entity class from a database

If you have an existing database architecture definition, you can use it to quickly create an object class in LINQ. Of course, the simplest way is to open the Server Browser in Visual Studio (Database Explorer in express edition-database browser) and select the table or view to be modeled, drag and Drop to the interface of the LINQ designer to automatically generate the corresponding object class:

Iii. Naming and de-plural

It should be noted that when using the LINQ designer to create entity classes based on the database architecture, it will automatically "pluralizes (de-plural form )". If the table name is "employees", the class name will be "employee". This naming method ensures that the modeling complies with. Net naming conventions. Of course, you can also rename the class and attribute through the designer or attribute bar:

Entities/attributes/associations have different naming functions from the database architecture in some cases, especially:

1. When the table/field name of the backend database changes. Because the entity model can have different names, you only need to update the ing rules without updating the application or the query code for the new table/field.

2. When the database architecture is not very "clean. For example, for the attributes of "au_lname" and "au_fname" in an object class, they can be renamed to "lastname" and "firstname" instead of being renamed in the database.

Iv. Link Association

When you drag an object from the server browser to the LINQ designer, Visual Studio automatically detects the primary/foreign key relationship between objects, create the default "relationship associations" between different entities based on them )". For example, when both the employeeaddress and address are added to the LINQ designer, an one-to-many Association is automatically expressed by an arrow:

The above association will make the employeeaddress object class have an address attribute, so that the address entity can be accessed through the given employeeaddress. Similarly, the address entity class will have a set of "employeeaddress" to obtain all the employeeaddress records in the address. Of course, you can also modify the attributes selected in the designer in the attribute bar:

5. latency/Inertia reading

By using LINQ, developers can specify whether the attributes in an object are pre-read or delayed/Inert during the first access. By selecting any attribute or association of an object, set the "delay loaded" attribute to true or false in the attribute column. For example, there is an object class such as document. The document field in the Document Table mapped to the database stores documents about the product description, we only want to read it when actually using it. Then, you can set its DeLoy loaded attribute to true to delay reading it:

Note: In addition to configuring the default value of the object attribute pre-read/delayed read, you can also overwrite the values of these attributes in the code when executing a LINQ query.

6. Use stored procedures

The SQL-based LINQ function allows you to model stored procedures into the datacontext class in the form of methods. For example, we define a stored procedure -- query the address information of the employee based on the employee ID:

 

In Visual Studio, we can drag the stored procedure to the LINQ designer and add a strongly typed method to call the stored procedure. If you drag a stored procedure to the "Address" Object of the LINQ designer, if the table value structure returned by the Stored Procedure matches the attribute in the object class, the designer declares that the stored procedure will return an ienumerable <address> result set:

7. Update/delete/insert data using Stored Procedures

By default, when an object is updated, deleted, or inserted, LINQ automatically generates an appropriate SQL expression. Of course, you can also use custom update/delete/Insert stored procedures, select the entity class in the LINQ designer, and click "... ", select the stored procedure to use:

Note: Changing the above settings is only purely handled at the LINQ ing layer, so the code in the program can continue to work without any changes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.