EF Framework - edmx file

           The above briefly introduced Entity FrameWork, here is the core of EF - edmx file.

           Create an edmx file in VisualStudio (the environment in this example is VisualStudio2012)

           1. New - ADO.NET Entity Data Model:

  

       2. When selecting a data model, because I have already established a table in the database before, we first choose to generate from the database (ie DB First). Build entity models to generate entity classes and database tables.

          

          3. Select the database connection:

          

         4. Select or set the connected server, authentication information, and database name:

          

         5. Note that because the connection string will be saved to the configuration file, select [Yes, include sensitive data in the connection string] here:

          

            6. Select the table for which we want to generate the entity:

          

          7. After everything is completed, as shown in the figure below, edmx has generated the corresponding entity according to the database table we selected. At the same time, in the solution explorer, under the T4 template Model.tt, we have also automatically generated Department and Employee entity class.

           

         At this point, the edmx file has been created.

 

        As mentioned above, the essence of the edmx file is an XML file, which is used to define the conceptual model, the storage model and the mapping between these models. Although the edmx file is opened by the entity designer by default, you can also right-click the Model.edmx file to open it with an XML text editor, and then you can see the true face of the edmx file:

         

        As can be seen from the code, edmx is roughly composed of three parts: SSDL, CSDL, and CS, which correspond to the analysis of database, entity, and mapping between database tables and entities. There are database tables, fields, etc. in SSDL. There are provisions for entity names, entity attributes, etc. in CSDL, and there are mappings between database tables and entities in CS. Summarizing the edmx file in one sentence is used to parse the storage model, the conceptual model and the mapping between the two. In fact, it is also the detailed performance of the picture above.

       As mentioned in the seventh step above, when the edmx template is generated, two classes Employee and Department corresponding to the entity are also generated. In the parent node of the entity class, there is also a Model.tt, which is the T4 template. The following code in the T4 template will understand that these two entity classes are actually generated by the T4 template according to the edmx configuration file. (The code will be very confusing when viewing the T4 template with VisualStudio, so you can install a T4 Editor plug-in and then learn the T4 template code. Plug-in download address: Click to jump )

 

[html]  view plain copy  
 
  1. <#@ include file="EF.Utility.CS.ttinclude"#><#@   
  2.  output extension=".cs"#><#  
  3.   
  4. const string inputFile = @"Model.edmx";  
  5. var textTransform = DynamicTextTransformation.Create(this);  
  6. var code = new CodeGenerationTools(this);  
  7. var ef = new MetadataTools(this);  
  8. var  typeMapper =  new TypeMapper (code, ef, textTransform.Errors);  
  9. var fileManager = EntityFrameworkTemplateFileManager.Create(this);  
  10. var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);  
  11. var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);  
  12.   
  13. if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))  
  14. {  
  15.     return string.Empty;  
  16. }  
  17.   
  18. WriteHeader(codeStringGenerator, fileManager);  
  19.   
  20. foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))  
  21. {  
  22.     fileManager.StartNewFile(entity.Name + ".cs");  
  23.     BeginNamespace(code);  
  24. #>  

Reprinted to: https://blog.csdn.net/huyuyang6688/article/details/41627669

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324941304&siteId=291194637