Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Generated Code Overview

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The ADO.NET Entity Framework provides tools to automatically generate an object layer based on the conceptual schema definition language (CSDL) content of an .edmx file. By default, the custom tool used by the ADO.NET Entity Data Model Designer (Entity Designer) to generate object layer code is the EntityModelCodeGenerator. For information about custom tools, see Implementing Single-File Generators. You can also use the EdmGen.exe command line tool. For more information, see How to: Use EdmGen.exe to Generate Object-Layer Code. The EntityModelCodeGenerator and EdmGen.exe command line tool use the EntityClassGenerator class to generate the source code.

The Entity Designer can also use text templates to generate customized object layer code. For more information, see How to: Customize Object-Layer Code Generation.

Generating Object-Layer Code

The default 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 Overview.

  • 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.

    Cc982041.note(en-us,VS.110).gifNote:
    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. Using the EdmGen.exe tool generates a Visual Basic or C# source code file with a name and location as specified in the command line.

Cc982041.note(en-us,VS.110).gifNote:
When object-layer code is generated by the default custom tool or by 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.

Cc982041.note(en-us,VS.110).gifNote:
The method that is generated based on a function import in the conceptual model will execute the corresponding stored procedure with the ExecuteFunction method. If you are using the overload that does not take the merge option parameter, the AppendOnly option will be used.

Cc982041.note(en-us,VS.110).gifNote:
Starting with Visual Studio 2010, the code generators set the LazyLoadingEnabled property on the ObjectContext to the value of the annotation:LazyLoadingEnabled annotation attribute that is defined in the conceptual model. By default, the annotation:LazyLoadingEnabled value is set to true.

Determining the Namespace of Generated Code

By default, the namespace of generated code is the namespace specified in the project's Application Page. If you specify the namespace in the Custom Tool Namespace property of the .edmx file, the specified namespace is used.

Some additional considerations apply depending on the project type, as follows:

  • For Visual Basic projects, 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 more information, see Determining the Default Namespace. If you specify the custom tool namespace, the Visual Studio compiler will prepend the project's root namespace to the name you specified. Even though the types are defined in the specified namespace, when you declare the types, you must import defaultnamespace.specifiednamespace. For example, if your project's root namespace is ProjectDefaultNamespace and in the Custom Tool Namespace property you specified TestNamespace, when you declare the types that are defined in the TestNamespace namespace, you must use: Import ProjectDefaultNamespace.TestNamespace.

  • For C# projects, if no namespaces have been specified for the Custom Tool Namespace property, 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 content of the .edmx file (for more information, see Schema Element (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.

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, as follows:

  • 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.

In This Section

See Also

Show:
© 2014 Microsoft. All rights reserved.