How to: Extend Code Generated by the O/R Designer

Code generated by the O/R Designer is regenerated when changes are made to the entity classes and other objects on the designer surface. Because of this code regeneration, any code that you add to the generated code is typically overwritten when the designer regenerates code. The O/R Designer provides the ability to generate partial class files in which you can add code that will not be overwritten. One example of adding your own code to the code generated by the O/R Designer is adding data validation to LINQ to SQL (entity) classes. For information, see How to: Add Validation to Entity Classes.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings.

Adding Code to an Entity Class

To create a partial class and add code to an entity class

  1. Open or create a new LINQ to SQL Classes file (.dbml file) in the O/R Designer. (Double-click the .dbml file in Solution Explorer/Database Explorer.)

  2. In the O/R Designer, right-click the class for which you want to add validation and then click View Code.

    The Code Editor opens with a partial class for the selected entity class.

  3. Add your code in the partial class declaration for the entity class.

Adding Code to a DataContext

To create a partial class and add code to a DataContext

  1. Open or create a new LINQ to SQL Classes file (.dbml file) in the O/R Designer. (Double-click the .dbml file in Solution Explorer/Database Explorer.)

  2. In the O/R Designer, right-click an empty area on the designer and then click View Code.

    The Code Editor opens with a partial class for the DataContext.

  3. Add your code in the partial class declaration for the DataContext.

See Also

Tasks

Walkthrough: Creating LINQ to SQL Classes (O/R Designer)

Walkthrough: Adding Validation to Entity Classes

Concepts

O/R Designer Overview

Other Resources

LINQ to SQL [LINQ to SQL]