In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This section describes how to manually construct a complex type (ComplexType) and the simple operation of a complex type.
In general, complex types are those that are made up of several simple types. For example: a Customer table with FristName and LastName fields, then 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 do this in EF.
Currently, because EF does not show support for complex types, we cannot design the complex types we need in the visual designer in VS. Therefore, we need to manually modify the solid model to support complex types of attributes. The main steps for modification are as follows:
1 > generate a solid model
2 > modify CSDL file
3 > modify msl file
4 > regenerate the model entity class
In the following introduction, I use NorthWind in the database and add the complex attribute Address for the entity class corresponding to the Customer table, where the complex attribute Address is a combination of Address,City,Region,Country and PostalCode.
Next, the specific operation steps are described:
Step 1: generate a solid model
The generation of entity models can be generated directly in the VS visual designer (if not, please refer to "Entity Framework Learning introduction to 1--EF basic Overview"). Or use the EdmGen tool to generate (the EdmGen tool is located under the system letter:\ WINDOWS\ Microsoft.NET\ Framework\ v3.5). The specific steps will not be repeated.
The solid model file I generated is: NorthwindEnites.edmx
Step 2: modify the csdl file
Generated the entity model, 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 to the VS inside to modify, so that it is more convenient to modify, after the modification is finished, the suffix can be changed back. )
Next, start manually modifying the csdl file, find the section on the csdl definition in the model file, find the definition section of the entity type named Customers, delete the original Address,City,Region,Country,PostalCode attribute definition, and then add an attribute named Address, as shown in the following code:
Next, you need to add a definition called the CommonAddress complex type, as shown in the following code:
At this point, the csdl part has been modified.
Third, modify the msl file:
Find the definition of the msl part and modify the mapping definition of the Customers part. The specific code is as follows (please note the ComplexProperty section):
At this point, the msl part has been modified.
Step 4: regenerate the entity class file.
We can use the EmdGen2 tool to recreate the entity class .cs file. The specific operations are as follows:
Copy the modified model file (edmx) to the same directory using edmgen2.exe, and then enter on the command line:
Edmgen2 / codegen cs NorthwindEnites.edmx
After executing this command, an NorthwindEnites.cs code file, that is, the code file for the entity class, is generated under the current folder. Rename the file to NorthwindEnites.Designer.cs (this step is mainly corresponding to edmx).
Then, add the NorthwindEnites.edmx and NorthwindEnites.Designer.cs files to the project.
At this point, the modification of the compound type is complete.
Following the same modification process, we can also add a complex type attribute of Address to Employees.
Next, let's look at the specific usage code:
> 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, I would like to add:
1. Complex types are not supported in the visual designer of VS, so you can no longer modify the model (edmx file) in the visual designer after modification.
2. A complex type cannot exist alone, it must be related to an entity.
3. Complex types cannot contain navigation attributes, such as navigating to entities or entity sets.
4. Complex types that have internal structures but do not have Key (primary key) attributes
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un