Udostępnij za pośrednictwem


Generated Code Overview (Entity Data Model Tools)

The ADO.NET Entity Framework provides tools to automatically generate an object layer based on the conceptual schema definition language (CSDL) of an .edmx file. By default, the custom tool used by the ADO.NET Entity Data Model Designer to generate object layer code is the EntityModelCodeGenerator. For information about custom tools, see Implementing Single-File Generators.

Generating Object Layer Code

The custom tool used to generate object layer code can be triggered in several ways:

  • Use the ADO.NET Entity Data Model Wizard. For more information, see Entity Data Model Wizard.

  • Use the Update Model Wizard. For more information see Update Model Wizard.

  • Run the tool that is specified in the Custom Tool .edmx file property. To do this, right-click an .edmx file in Solution Explorer and select Run Custom Tool. For more information about .edmx file properties, see .edmx File Properties.

  • Save an .edmx file.

  • Tab away from an .edmx file that has been changed. Note that this includes tabbing away from a changed .edmx file that is open in the Entity Designer.

  • Build a project that has an .edmx file that has been changed.

    Note

    The options above generate or overwrite a Visual Basic or C# source code file with the following name: <model name>.Designer.vb or <model name>.Designer.cs. This file is added to the .edmx node in Solution Explorer.

  • Use the EdmGen.exe command line tool. For more information, see How to: Use EdmGen.exe to Generate Object Model Files (Entity Framework).

    Note

    This method generates a Visual Basic or C# source code file with a name and location as specified in the command line.

Note

When object layer code is generated with the custom tool or with the EdmGen.exe tool, existing object layer code is overwritten. For information about how to avoid overwriting customizations to the object layer, see How to: Customize Generated Data Objects (Entity Framework).

Determining the Namespace of Generated Code

The namespace of generated code depends on the project type, as follows:

  • For Visual Basic projects, the namespace of generated code will be the namespace specified in the Custom Tool Namespace property of the .edmx file, if a namespace has been specified (for more information, see .edmx File Properties). Otherwise, the namespace of generated code will be the root namespace of the project as specified in the project's Application Page.

    Note

    If no value has been set for the Custom Tool Namespace property and the project has an empty namespace, the namespace of the generated code will be the conceptual model namespace as specified in the .edmx file.

  • For C# projects, the namespace of generated code will be the namespace the Custom Tool Namespace conceptual model property, if one has been specified (for more information, see .edmx File Properties). Otherwise, the namespace of generated code will be the default namespace of the project (as specified in the project's Application Page) plus the folder path of the .edmx file location. For example, if the path of the .edmx file relative to the project's root directory is Folder1/Folder2/Model.edmx, the namespace of the generated code will be ProjectDefaultNamespace.Folder1.Folder2.

  • For ASP.NET Website projects, if the .edmx file is in the root of the App_Code folder, the namespace of generated code will be the conceptual model’s namespace as defined in the CSDL section of the .edmx file (for more information, see Namespace Attribute (CSDL)). If the .edmx file is in a sub-folder of the App_Code folder, the namespace of the generated code will be the folder path relative to the App_Code folder. For example, if the .edmx file is in App_Code/Folder1/Folder2, the namespace of generated code will be Folder1.Folder2.

For more information, see Determining the Default Namespace.

Working with Multiple .edmx Files in a Project

When a project contains multiple .edmx files that are based on databases with common table names, the generated code can contain conflicting class names. The circumstances under which these conflicts arise and how the conflicts are resolved depends on the project type:

  • For C# projects, conflicting class names can appear in generated code if multiple .edmx files based on databases with common table names are in the same project folder. To resolve a conflict, specify a unique value for the Custom Tool Namespace property of each .edmx file or put each .edmx file in a separate project sub-folder.

  • For Visual Basic projects, conflicting class names can appear in generated code if multiple .edmx files based on databases with common table names are anywhere in the project. To resolve a conflict, specify a unique value for the Custom Tool Namespace property of each .edmx file.

  • For ASP.NET Website projects, conflicting class names can appear in generated code if multiple .edmx files based on databases with common table names are in the App_Code folder or in the same sub-folder of the App_Code directory. To resolve a conflict, put each .edmx file in a separate sub-folder of the App_Code directory.

Note that the resolution in each case changes the namespace of the code generated for each .edmx file; existing application code that consumes the generated classes might need to be updated.

Extending Partial Classes

When data classes are generated by the Entity Data Model tools, they are implemented in partial classes. You can add functionality to objects by extending the generated partial data classes. Having partial classes enables you to extend these classes with custom methods and properties in a separate source file without having to worry about losing your customization when the generated files are refreshed. For more information see, How to: Customize Generated Data Objects (Entity Framework).

See Also

Concepts

.edmx File Overview