Ado. Net entityobject Generator template

Source: Internet
Author: User

This topic provides an overview"ADO. Net entityobject generator"Templates also demonstrate how to customize text templates."ADO. Net entityobject generator"The template generates the typed objectcontext and entity classes (Object-Layer Code) derived from entityobject ).

"ADO. Net entityobject generator"The code generated by the template is the same as the default code generated by the Entity Designer. The ADO. Net entityobject Generator template consists of a text template file:<Model name>. TT.<Model name>The. tt template outputs a source file.<Model name>. CS (or. VB). The source file appears under <model name>. TT in Solution Explorer.

<Model name>. tt file code Overview

First, the Code uses built-in instructions to instruct the Text Template processing engine to process the template. A text template includes a. ttinclude file, which contains a utility class that helps code generation. For more information about the. ttinclude file, see the Entity Framework utility. ttinclude file.

<#@ template language="VB" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.VB.ttinclude"#> <#@ output extension = ".vb" #> 
  1. <# @ Template Language = "C #" DEBUG = "false" hostspecific = "true" #>
  2. <# @ Include file = "Ef. Utility. cs. ttinclude" #>
  3. <# @ Output extension = ". cs" #>

Then, it createsUsersettingsType instance, which allows users to specify how code should be written. For example, this type determines whether the camel case format is used for the field name, or whetherAddtoMethod is added to the typed object context class that generates code.

<# Dim userSettings As UserSettings = New UserSettings With _ { _ .SourceCsdlPath = "SchoolModel.edmx", _ .ReferenceCsdlPaths = new string () {}, _ .FullyQualifySystemTypes = True, _ .CreateContextAddToMethods = True, _ .CamelCaseFields = False _ } ApplyUserSettings(userSettings) #> 
  1. <#
  2. Usersettings =
  3. New usersettings
  4. {
  5. Sourcecsdlpath = @ "schoolmodel. edmx ",
  6. Referencecsdlpaths = new string [] {},
  7. Fullyqualifysystemtypes = true,
  8. Createcontextaddtomethods = true,
  9. Camelcasefields = false,
  10. };
  11. Applyusersettings (usersettings );
  12. #>

The Code then instantiates and initializes the helper class defined in the. ttinclude file. Some local variables will also be initialized.

<# Dim loader As New MetadataLoader(Me) Dim ef As New MetadataTools(Me) Dim region As New CodeRegion(Me) Dim code As New CodeGenerationTools(Me) With {.FullyQualifySystemTypes = userSettings.FullyQualifySystemTypes, .CamelCaseFields = userSettings.CamelCaseFields} ItemCollection = loader.CreateEdmItemCollection(SourceCsdlPath, ReferenceCsdlPaths.ToArray()) ModelNamespace = loader.GetModelNamespace(SourceCsdlPath) Dim namespaceName As String = code.VsNamespaceSuggestion() UpdateObjectNamespaceMap(namespaceName) #> 
  1. <#
  2. Metadataloader loader = new metadataloader (this );
  3. Metadatatools EF = new metadatatools (this );
  4. Coderegion region = new coderegion (this );
  5. Codegenerationtools code = new codegenerationtools (this) {fullyqualifysystemtypes = usersettings. fullyqualifysystemtypes, camelcasefields = usersettings. camelcasefields };
  6. Itemcollection = loader. createedmitemcollection (sourcecsdlpath, referencecsdlpaths. toarray ());
  7. Modelnamespace = loader. getmodelnamespace (sourcecsdlpath );
  8. String namespacename = code. vsnamespacesuggestion ();
  9. Updateobjectnamespacemap (namespacename );
  10. #>

After initialization, use the hybrid text block and code block to generate the text of the output file.Foreach(InVisual BasicIsFor each) Loops are used based onGetsourceschematypesThe metadata information returned by the Helper function is written into text.GetsourceschematypesThe Helper Function<Model name>Define in the. tt file and call the getitems method to obtain all specified types of items from the item set. Below is<Model name>. CS or<Model name>Description of the code of the. VB file:

  1. EDM link metadata.
  2. TypeObjectcontext. Class definition includes: constructor overload, objectset attribute,AddtoMethod (ifUsersettings. createcontextaddtomethodsSetTrue) And Function Import method (if any such method is defined in the conceptual model ).
    <Model name>The following code in the. tt file is typedObjectcontextClass.
    <#=Accessibility.ForType(container)#> Partial Class <#=code.Escape(container)#> Inherits ObjectContext 
    1. <# = Accessibility. fortype (container) #> partial class <# = code. Escape (container) #>: objectcontext

    If the container name isSchoolEntitiesIn<Model name>. CS or<Model name>Generate the following code in the. VB file:

    Public Partial Class SchoolEntities Inherits ObjectContext 
    1. Public partial class schoolentities: objectcontext
  3. Object Type class. These classes are derived fromEntityobjectAnd includes how to map object types in the Object layer to the features of object types in the conceptual model. Object classes include factory methods, primitive attributes, complex attributes, and navigation attributes.
  4. Complex type class. These classes are derived from complexobject and include features that define how to map complex types in the Object layer to complex types in the conceptual model.

Then, define the Helper function. In a text template, the Helper function is included in the class function block. You can use<# +And#>To indicate the class function flag.

Custom object Layer Code

If you want to customize the method for generating the object-layer code, you must modify the. tt file. You may need to modify the context name of a typed object, add new or modify existing attributes or features for the object type, or inherit the object type from an interface. To customize the object-layer code, you can modify the text block in the. tt file (This text block is located in<#And#>Tag external ).

To change the context name of a typed object, go<#=code.Escape(container)#>Add corresponding words (for example,My). Then, if the name of the container in the. edmx file isSchoolEntities, The name of the container in the generated code will beMySchoolEntities..

You can also changeUsersettingsThe value of a field of the type is derived from the code generated by the definition. For example, if you do not want the generated code to have a fully qualified system type, you canFullyQualifySystemTypesSetFalse.

For more information, see How to: custom object Layer Code Generation (Entity Data Model Designer ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.