| title | Extend Code Generated by the O-R Designer |
|---|---|
| description | Review how to extend code that's generated by Object Relational Designer (O/R Designer). Add code to an entity class. Add code to a DataContext. |
| ms.date | 11/04/2016 |
| ms.topic | how-to |
| author | ghogen |
| ms.author | ghogen |
| ms.subservice | data-tools |
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 is not 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 more information, see How to: Add validation to entity classes.
[!INCLUDEnote_settings_general]
-
Open or create a new LINQ to SQL Classes file (
.dbmlfile) in the O/R Designer. (Double-click the.dbmlfile in Solution Explorer or Database Explorer.) -
In the O/R Designer, right-click the class for which you want to add validation, and then select View Code.
The Code Editor opens with a partial class for the selected entity class.
-
Add your code in the partial class declaration for the entity class.
-
Open or create a new LINQ to SQL Classes file (
.dbmlfile) in the O/R Designer. (Double-click the.dbmlfile in Solution Explorer or Database Explorer.) -
In the O/R Designer, right-click an empty area on the designer, and then select View Code.
The Code Editor opens with a partial class for the DataContext.
-
Add your code in the partial class declaration for the DataContext.