Udostępnij za pośrednictwem


Typical Steps for Using LINQ to SQL

To implement a LINQ to SQL application, you follow the steps described later in this topic. Note that many steps are optional. It is very possible that you can use your object model in its default state.

For a really fast start, use the Object Relational Designer to create your object model and start coding your queries. Object Relational Designer (O/R Designer)
Object Relational Designer (O/R Designer) 

Creating the Object Model

The first step is to create an object model from the metadata of an existing relational database. The object model represents the database according to the programming language of the developer. For more information, see The LINQ to SQL Object Model.

1. Select a tool to create the model.

Three tools are available for creating the model.

  • The Object Relational Designer

    This designer provides a rich user interface for creating an object model from an existing database. This tool is part of the Visual Studio IDE, and is best suited to small or medium databases. Object Relational Designer (O/R Designer)
    Object Relational Designer (O/R Designer)

  • The SQLMetal code-generation tool

    This command-line utility provides a slightly different set of options from the O/R Designer. Modeling large databases is best done by using this tool. For more information, see Code Generation Tool (SqlMetal.exe).

  • A code editor

    You can write your own code by using either the Visual Studio code editor or another editor. We do not recommend this approach, which can be prone to errors, when you have an existing database and can use either the O/R Designer or the SQLMetal tool. However, the code editor can be valuable for refining or modifying code you have already generated by using other tools. For more information, see How to: Customize Entity Classes by Using the Code Editor (LINQ to SQL).

2. Select the kind of code you want to generate.

  • A C# or Visual Basic source code file for attribute-based mapping.

    You then include this code file in your Visual Studio project. For more information, see Attribute-Based Mapping (LINQ to SQL).

  • An XML file for external mapping.

    By using this approach, you can keep the mapping metadata out of your application code. For more information, see External Mapping Reference (LINQ to SQL).

    Note

    The O/R Designer does not support the generation of external mapping files. You must use the SQLMetal tool to implement this feature.

  • A DBML file, which you can modify before generating a final code file.

    This is an advanced feature.

3. Refine the code file to reflect the needs of your application.

For this purpose, you can use either the O/R Designer or the code editor.

Using the Object Model

The following illustration shows the relationship between the developer and the data in a two-tier scenario. For other scenarios, see N-Tier and Remote Applications with LINQ to SQL.

LINQ to SQL Object Model

Now that you have the object model, you describe information requests and manipulate data within that model. You think in terms of the objects and properties in your object model and not in terms of the rows and columns of the database. You do not deal directly with the database.

When you instruct LINQ to SQL to either execute a query that you have described or call SubmitChanges() on data that you have manipulated, LINQ to SQL communicates with the database in the language of the database.

The following represents typical steps for using the object model that you have created.

1. Create queries to retrieve information from the database.

For more information, see Query Concepts in LINQ to SQL and Query Examples (LINQ to SQL).

2. Override default behaviors for Insert, Update, and Delete.

This step is optional. For more information, see Customizing Insert, Update, and Delete Operations (LINQ to SQL).

3. Set appropriate options to detect and report concurrency conflicts.

You can leave your model with its default values for handling concurrency conflicts, or you can change it to suit your purposes. For more information, see How to: Specify Which Members are Tested for Concurrency Conflicts (LINQ to SQL) and How to: Specify When Concurrency Exceptions are Thrown (LINQ to SQL).

4. Establish an inheritance hierarchy.

This step is optional. For more information, see Inheritance Support (LINQ to SQL).

5. Provide an appropriate user interface.

This step is optional, and depends on how your application will be used. Object Relational Designer (O/R Designer)
Object Relational Designer (O/R Designer)

6. Debug and test your application.

For more information, see Debugging Support (LINQ to SQL).

See Also

Concepts

Getting Started (LINQ to SQL)

Other Resources

Creating the Object Model (LINQ to SQL)

Stored Procedures (LINQ to SQL)