(LINQ) (1) Typical steps for using LINQ to SQL

Source: Internet
Author: User

This article serves as the basis for learning LINQ. All content comes from the learning materials of ms linq.

As a component of. NET Framework Version 3.5, LINQ to SQL provides a runtime infrastructure for using relational data as object management.

Language integration query (LINQ) is an innovative feature introduced in Visual Studio 2008 and. NET Framework 3.5. It builds a bridge between the object field and the data field.

Traditionally, data-based queries are represented by simple strings, but are not supported by compile-time type checks or intelliisense. In addition, you must learn a different query language for the following data sources: SQL database, XML documents, various Web services, and so on. By using LINQ, queries become the first-class language structures in C # and Visual Basic. You can use language keywords and familiar operators to write queries for a strongly typed object set.

 

Typical steps for using LINQ to SQL

Create an object model

The first step is to create an object model with the metadata of an existing relational database. The object model represents the database according to the programming language used by the developer.

1.Select a tool for creating a model.

There are three tools available for creating models.

  • Object link designer.

This designer provides a rich user interface for creating an object model from an existing database. This tool is part of Visual Studio IDE and is best suited for small or medium-sized databases.

· SQLMetal Code Generation Tool

This command line utility provides a set of options slightly different from the O/R designer. It is best to use this tool to model large databases.

For example, my database is Sqlserver 2008 and the database name is lxPower.


Generate a. dbml file containing the extracted SQL metadata:

Sqlmetal/server: localhost/database: yourdatabase/user: sa/password: XXX/dbml: mymeta. dbml

 

Generate Source Code directly based on SQL metadata:

Sqlmetal/server: localhost/database: yourdatabase/namespace: meetweb/code: d:/linqtest. cs/language: csharp

 

Copy the files generated above to vs2010 and include them in your project. Then, use vs to generate the. dbml. layout file.

 

· Code editor

You can use the Visual Studio code editor or another editor to write your own code. We recommend that you do not use this method when you have an existing database and can use the O/R designer or SQLMetal tool, because this method is prone to errors. However, the Code Editor is useful in improving or modifying the code that you have generated using other tools.

You can manually generate the basic class or other design tools, but this method is only suitable for reading data. Other actions such as adding and deleting cannot be directly used.

 

2. Select the code type you want to generate.

· C # or Visual Basic source code file for property-based ing.

Add the code file to your Visual Studio project.

· XML file used for external ing.

By using this method, you can place the ing metadata outside the application code.

DBML file, which can be modified before the final code file is generated. 

3. Improve the code file to reflect your application needs.

To do this, you can use the O/R designer or code editor.

 

Use object model

1. Create a query to retrieve information from the database. 2. Rewrite the default Insert, Update, and Delete actions.

This step is optional.

LINQ to SQL does not support and cannot identify cascading deletion operations. If you want to delete a bound row from the table, you must set it in the foreign key constraint of the database.ON DELETE CASCADERules, or use your own code to first Delete the sub-objects that prevent the deletion of the parent object. Otherwise, an exception is thrown.

3. Set appropriate options to detect concurrency conflicts with reports.

You can retain the default value of the model for processing concurrency conflicts, or change it as needed. Yes.

4. Establish an inheritance hierarchy.

This step is optional.

5. provide an appropriate user interface.

This step is optional and depends on how your application is used.

 

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.