EF supports implementation of complex types

This section describes how to manually construct complex types and simple operations for complex types.
Generally, complex types refer to those types that are composed of several simple types. For example, if a Customer table has FristName and LastName fields, the corresponding Customer entity class will have FristName and LastName attributes. When we want to combine FirstName and LastName into a property called CustomerName, we need to use complex types if we want to achieve this in EF.
At present, because EF can't display supporting complex types, we can't design the complex types we need in the visual designer in VS. So we need to manually modify the entity model to support complex types of attributes. The main steps of modification are as follows:

1 > Generating Entity Model
2 > Modify CSDL file
3 > Modify the msl file
4 > Renewing Model Entity Classes

In the following introduction, I use NorthWind as the database and add the complex attribute Address to the entity class corresponding to the Customer table. The complex attribute Address is composed of Address,City,Region,Country and Postal Code.
Following is a description of the specific steps of operation:

Step 1: Generate an entity model
The generation of entity models can be directly generated by VS visual designer (if not, refer to Entity Framework Learning Preliminary 1 - Basic Survey of EF). Or use the EdmGen tool (the EdmGen tool is located below the system disc character: WINDOWS Microsoft. NET Framework v3.5). The specific steps will not be repeated.
The entity model file I generated is: NorthwindEnites.edmx

Step 2: Modify the csdl file
After the entity model is created, 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 for modification, which is more convenient to modify. After modification, you can change the suffix back. )
Next, start modifying the CSDL file manually, find the part of the model file about the definition of csdl, then find the definition section of entity type named Customers, delete the original definition of Address,City,Region,Country, PostalCode attribute, and then add an attribute 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="40" Unicode="true" FixedLength="false" />
  <Property Name="ContactName" Type="String" MaxLength="30" Unicode="true" FixedLength="false" />
  <Property Name="ContactTitle" Type="String" MaxLength="30" Unicode="true" FixedLength="false" />
  <Property Name="Address" Type="NorthwindModel.CommonAddress" Nullable="false"></Property>
  <Property Name="Phone" Type="String" MaxLength="24" Unicode="true" FixedLength="false" />
  <Property Name="Fax" Type="String" MaxLength="24" 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 of a complex type called CommonAddress, which is as follows:

<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>

So far, the csdl part has been modified.

The third step is to modify the msl file:
Find the definition of the msl section and modify the mapping definition of the Customers section. The code is as follows (note the ComplexProperty section):

<EntitySetMapping Name="Customers">
    <EntityTypeMapping TypeName="IsTypeOf(NorthwindModel.Customers)">
      <MappingFragment StoreEntitySet="Customers">
    <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
    <ScalarProperty Name="CompanyName" ColumnName="CompanyName" />
    <ScalarProperty Name="ContactName" ColumnName="ContactName" />
    <ScalarProperty Name="ContactTitle" ColumnName="ContactTitle" />
    <ComplexProperty Name="Address" TypeName="NorthwindModel.CommonAddress">
      <ScalarProperty Name="Address" ColumnName="Address" />
      <ScalarProperty Name="City" ColumnName="City" />
      <ScalarProperty Name="Region" ColumnName="Region" />
      <ScalarProperty Name="PostalCode" ColumnName="PostalCode" />
      <ScalarProperty Name="Country" ColumnName="Country" />
    </ComplexProperty>
    <ScalarProperty Name="Phone" ColumnName="Phone" />
    <ScalarProperty Name="Fax" ColumnName="Fax" />
      </MappingFragment>
    </EntityTypeMapping>
  </EntitySetMapping>

So far, the msl part has been modified
Step 4: Regenerate entity class files.
We can use the EmdGen2 tool to reassemble the entity class. cs file. The specific operation is as follows:
Copy the modified model file (edmx) to the same directory using edmgen2.exe, and then enter it on the command line:
Edmgen2 /codegen cs NorthwindEnites.edmx
After executing this command, a NorthwindEnites.cs code file, which is the code file of the entity class, is generated under the current folder. Change the name of the file to NorthwindEnites.Designer.cs (this step corresponds mainly to edmx).
Then, add the NorthwindEnites.edmx and NorthwindEnites.Designer.cs files to the project.
So far, the modification of the composite type has been completed.
Following the same modification process, we can add an Address complex type attribute to Employees.
Next, let's look at the specific code used:

> Query:

public void TestAddress()
{    using (var db = new NorthwindModel.NorthwindEntities1())
    {
    Console.WriteLine("Get Five customer addresss :");    var cts = db.Customers.Take(5);    foreach (var c in cts)
    {
        Console.WriteLine("Address:{0},Country:{1},City:{2},PostalCode:{3}", c.Address.Address, c.Address.Country, c.Address.City, c.Address.PostalCode);
    }
    Console.WriteLine("Get Five Employess address:");    var emp = db.Customers.Take(5);    foreach (var c in emp)
    {
        Console.WriteLine("Address:{0},Country:{1},City:{2},PostalCode:{3}", c.Address.Address, c.Address.Country, c.Address.City, c.Address.PostalCode);
    }
    }
}

> Add:

public void AddTest()
{    using (var db = new NorthwindModel.NorthwindEntities1())
    {    var customer = new NorthwindModel.Customers
    {
        CustomerID = "2009",
        CompanyName = "Complex Company",
        ContactName = "xray2005",
        Address = new NorthwindModel.CommonAddress
        {
        Address = "SiChuan,China",
        City = "ChengDou",
        Country = "China",
        PostalCode = "610041",
        Region = "Chenghua"
        }
    };
    db.AddToCustomers(customer);
    db.SaveChanges();    var cst = db.Customers.FirstOrDefault(c => c.CustomerID == "2009");
    Assert.IsNotNull(cst);             
    Console.WriteLine("CustomerID:{0},CompanyName:{1},ContactName:{2},City:{3},Country:{4}", cst.CustomerID, cst.CompanyName, cst.ContactName, cst.Address.City, cst.Address.Country);
    }
}

> Conditional Query:

public void QueryTest()
{    using (var db = new NorthwindModel.NorthwindEntities1())
    {    var cst = db.Customers.FirstOrDefault(c => c.Address.City == "ChengDou");
    Assert.IsNotNull(cst);       
    Console.WriteLine("CustomerID:{0},CompanyName:{1},ContactName:{2},City:{3},Country:{4}", cst.CustomerID, cst.CompanyName, cst.ContactName, cst.Address.City, cst.Address.Country);
    }
}

Finally, it is added that:
1. In Visual Designer of VS, complex types are not supported, so it is impossible to modify the model (edmx file) in Visual Designer after modification.
2. Complex types cannot exist alone. They must be related to an entity.
3. Complex types cannot contain navigation attributes, such as navigating to entities or entity sets.
4. Data types of complex types with internal structure but without Key (primary Key) attributes


Keywords: Attribute Database Windows xml

Added by nosheep on Wed, 03 Jul 2019 01:28:00 +0300