Implementation of Entity Framework Learning intermediate 1-EF support complex types

Source: Internet
Author: User

This section describes how to manually construct complex types (ComplexType) and simple operations for complex types.

Typically, complex types are those that are grouped by a few simple types. For example: a Customer table, which has fristname and LastName fields, then the corresponding customer entity class will have the Fristname and LastName properties. When we want to synthesize FirstName and LastName into a named CustomerName attribute, then if we want to do that in the EF, then we need to use a complex type.

Currently, because EF cannot show support for complex types, we cannot design the complex types we need in the visual designer in vs. So, we need to manually modify the entity model so that it supports properties of complex types. The main steps to modify are the following:

L Generate Entity Models

L Modify the CSDL file

L Modify the MSL file

L Regenerate Model entity classes

In a subsequent introduction, I used the database to use Northwind and to add complex property addresses to the corresponding entity classes for the Customer table, where the complex property address was addressed by Address,city,region, Country and PostalCode this several combinations.

Below, describe the specific steps to be done:

Step one: Generate the Entity model

The production of the entity model can be generated directly by the VS visual designer (if not, refer to the basic overview of the Entity Framework Learning Primary article 1--EF). Or use the Edmgen tool to generate (Edmgen tool is located at: System Letter: \windows\microsoft.net\framework\v3.5 below). The concrete steps are no longer mentioned.

The entity model file that I produced is: northwindenites.edmx

Step two: Modify the CSDL file

After the entity model has been generated, we use Notepad or other text editing tools to open the entity model, (TIP: You can change the entity model suffix. edmx to. xml, and then drag the entity model file directly into vs to modify it, so it is convenient to modify, after the modification, the suffix can be changed back.) )

Next, start by manually modifying the CSDL file, finding the section in the model file about the CSDL definition, and then finding the definition section with the entity type name customers, deleting the original Address,city,region,country,postalcode attribute definition, Then add a property named Address, as shown in the following code:

<entitytype name= "Customers" >
<Key>
<propertyref name= "CustomerID"/>
</Key>
<property name= "CustomerID" type= "String" nullable= "false" Maxlength= "5" unicode= "true" fixedlength= "true"/>
<property name= "CompanyName" type= "String" nullable= "false" Maxlength= "M" unicode= "true" Fixedlength= "false"/ >
<property name= "ContactName" type= "String" maxlength= "a" unicode= "true" Fixedlength= "false"/>
<property name= "ContactTitle" type= "String" maxlength= "a" unicode= "true" Fixedlength= "false"/>
<property name= "Address" type= "Northwindmodel. Commonaddress "nullable=" false ></Property>
<property name= "Phone" type= "String" maxlength= "a" unicode= "true" Fixedlength= "false"/>
<property name= "Fax" type= "String" maxlength= "unicode=" "true" Fixedlength= "false"/>
<navigationproperty name= "Orders" relationship= "Northwindmodel. FK_Orders_Customers "fromrole=" Customers "torole=" Orders "/>
<navigationproperty name= "Customerdemographics" relationship= "Northwindmodel". Customercustomerdemo "fromrole=" Customers "torole=" Customerdemographics "/>"
</EntityType>

Next, you need to add a definition named Commonaddress complex Type, the following code:

<ComplexType Name="CommonAddress">
<Property Name="Address" Type="String" MaxLength="60" Unicode="true" FixedLength="false" />
<Property Name="City" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
<Property Name="Region" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
<Property Name="PostalCode" Type="String" MaxLength="10" Unicode="true" FixedLength="false" />
<Property Name="Country" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
</ComplexType>

At this point, the CSDL section has been modified.

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.