Entity Framework 6 Recipes Chinese translation series (39), entityframework

Source: Internet
Author: User
Tags metadate

Entity Framework 6 Recipes Chinese translation series (39), entityframework
For the original intention of translation and why I chose Entity Framework 6 Recipes, please refer to the configuration model 7-3 at the beginning of this series.

Problem

  You want to know the various options in the configuration model.

Solution

When you add an ADO. NET Entity Data Model to your project, the Entity Framework sets the Build Acion attribute of the. edmx file to Entity Deploy. In addition, set Metadate Artifact Processing to Embed in Output Assembly ). When you compile a project, Entity Deploy extracts three parts from the. edmx file and puts them in three different files. The CSDL part is extracted to the Model. csdl file, the MSL part is extracted to the Model. msl file, and the SSDL part is extracted to the Model. ssdl file. Because of the "embedded output assembly" option, these three files will be embedded into the Assembly as resources.

If you change the metadata project Processing (Metadate Artifact Processing) to "Copy to Output Directory", the three files model. * will be copied to the Output Directory of the Assembly. They are not embedded into the Assembly as resources.

Principle

The. edmx file contains all three layers: Conceptual Model layer, ing layer, and storage logic layer. It also contains the designer used to manage the data in the design window. At runtime, the Entity Framework uses each layer separately.. The edmx file is a convenient container for design.The configuration of the model depends on all layers in the model. These layers can be embedded into the assembly and stored in the file, as can be seen in section 7-2, obtain and complete the creation of the MetadataWorkspace instance from another data source.

If the metadata project Processing (Metadate Artifact Processing) is set to "embedded Output Assembly", you will see your configuration file App. config or web. the connection string in config contains a metadata tag, which may look like the following:

Metadata = res: // */Recipe3.csdl | res: // */Recipe3.ssdl | res: // */Recipe3.msl;

These symbols indicate the search path corresponding to the model layer embedded in the Assembly. If you change the metadata project Processing (Metadate Artifact Processing) to "Copy to Output Directory", you will see that the connection string will change like the following:

Metadata =. \ Recipe3.csdl |. \ Recipe3.ssdl |. \ Recipe3.msl;

These symbols indicate the path of the file corresponding to each model layer.

When you embed model layer definition data into an assembly as a resource, you are not limited by the connection string syntax that only references executable assembly. As shown in Table 7-1, you may reference the structure of data embedded in the model layer in other programs.

Table 7-1.Connection string syntax for Loading Model layer data

 

7-4 Deployment model

Problem

  When you import a model from a database, you want to use the singular and plural services of the Entity Framework.

Solution

Suppose you have a database table 7-3.

Figure 7-3 database tables Employees and Tasks

Note that tables in Figure 7-3 use the plural form. This is a common usage of many databases. Some DBAs believe that all tables should use complex forms, while others oppose this practice. There are also a few DBAs that do not seem to use any of the above practices, but use them together. In your opinion, you may use the singular form for the table name corresponding to the model object. The plural service provided by the Entity Framework automatically generates an appropriate plural form of Entity names from the singular form.

To use the singular and plural service when importing tables, you must check the single and plural forms of the generated object name in the last step (7-4) of the Object Data Model Wizard. By default, this check box is selected.

Figure 7-4. Enable the plural Service

 

Figure 7-5 shows that the single and multiple numbers service is not used when the table in Figure 7-3 is imported. Note that the entity name uses the table name directly and maintains the plural form. Figure 7-6 shows that when the same table modeling is imported, the entity name uses the singular form of the table name after the singular and plural services are enabled.

Figure 7-5. When you import a table in Figure 7-3 to create a model, the singular and plural services are not used.

 

Figure 7-6. When you import a table in Figure 7-3 to create a model, enable the single and multiple numbers service.

 

Principle

Many developers prefer to use the entity name format shown in Figure 7-6. (Please refer to the name in bold above the entity ). They use not only the Entity names in the singular form, but also the navigation attributes in the Task entity, not the complex form shown in Figure 7-5. In both cases, the navigation attribute is an entity reference, not a set. The complex form in Figure 7-5 is easy to cause confusion.

If our table name is in the singular form, the singular and plural services will be correctly plural Based on the Set navigation attribute and the object set name. This is to take care of the DBA community where the other half uses the singular form table name.

You can modify the attribute "express new object in the plural form" to set the on/off (enable/disable) status when adding a new entity to the model ).

  You can use this singular and plural service outside the physical framework.In the System. data. entity. in the Design namespace, you must reference System. data. entity. design. dll. NET Framwork4 Client Profile changed. NET Framwork4 ). This modification operation is in the properties of the project. Code listing 7-3 demonstrates how the singular and plural service is single-plural for the words "Person" and "People.

Code List 7-3.Use the singular and plural Service

1             var service = PluralizationService.CreateService(new CultureInfo("en-US"));2             string person = "Person";3             string people = "People";4             Console.WriteLine("The plural of {0} is {1}", person,5              service.Pluralize(person));6             Console.WriteLine("The singular of {0} is {1}", people,7              service.Singularize(people));    

The output of code listing 7-3 is as follows:

The plural of Person is PeopleThe singular of People is Person

 

 

Entity Framework exchange QQ group: 458326058. You are welcome to join us.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/VolcanoCloud/

 

Related Article

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.