Entity Framework learning notes -- edmx file, entityedmx

Source: Internet
Author: User

Entity Framework learning notes -- edmx file, entityedmx

The Entity FrameWork is briefly introduced above. Here we will talk about the edmx file, the core of EF.

Create an edmx file in Visual Studio (in this example, the environment is VisualStudio2012)

1. Create the-ADO. NET Object Data Model:

2. When selecting a data model, because I have already created a table in the database, we should First generate the table from the database (that is, DB First). If we select an empty model, you can create an object model to generate an object class and database table without creating a database.

3. Select database connection

4. Select or set the connection server, verification information, and Database Name:

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

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

7. After everything is complete, for example, edmx has generated the corresponding entity based on the database table we selected, and the T4 template Model in solution Resource Manager. in tt, the Department and Employee entity classes are generated automatically.

By now, the edmx file has been created.

 

As mentioned above, the edmx file is essentially an XML file that defines the ing between conceptual models, storage models, and these models. Although the edmx file is opened by default in the form of an entity designer, you can also right-click the Model. edmx file to open it in an XML text editor. Then you can see the true edmx file:

From the code, we can see that edmx is roughly composed of three parts: SSDL, CSDL, and C-S.Database,Entity,Ing between database tables and entitiesThese three aspects of the parsing, SSDL has on the database table, fields and other provisions, CSDL has on the entity name, object attributes, such as the provisions of the C-S has on the database table and entity ing. In a word, the edmx file is used to parse the storage model, conceptual model, and ing between the two. In fact, it is also the detailed performance of the image above.

As mentioned in Step 7 above, when an edmx template is generated, two classes of Employee and Department corresponding to the object are also generated. There is also a Model on the parent node of the object class. tt: This is the T4 template. After reading the Code in the following T4 template, we will understand that the two entity classes are actually generated by the T4 template according to the edmx configuration file. (In the era of viewing T4 templates with Visual Studio, codes are messy. Therefore, you can install a T4 Editor plug-in to learn the T4 template code. Plug-In: Click to jump)

 

 
<#@ include file="EF.Utility.CS.ttinclude"#><#@    output extension=".cs"#><#    const string inputFile = @"Model.edmx";  var textTransform = DynamicTextTransformation.Create(this);  var code = new CodeGenerationTools(this);  var ef = new MetadataTools(this);  var typeMapper = new TypeMapper(code, ef, textTransform.Errors);  var fileManager = EntityFrameworkTemplateFileManager.Create(this);  var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);  var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);    if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))  {      return string.Empty;  }    WriteHeader(codeStringGenerator, fileManager);    foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))  {      fileManager.StartNewFile(entity.Name + ".cs");      BeginNamespace(code);  #>
 

 

 

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.