Problem:
There is a self-referenced table in the database. you want to create a self-referenced object-link model.
Solution Process:
Assume that the self-referenced table structure is 2-5-1.
Figure 2-5-1
To create a self-Reference Model, follow these steps:
- Right-click your project and add a new model. Select Add new item, and then select ADO. NET Entity Data Model.
- Select generate from database and click Next.
- Use the create Wizard to select an existing database connection or create a new one.
- Select the PictureCategory table from the "select your database object" dialog box. Make sure that the two check boxes at the bottom are selected. Click Finish.
The Wizard will help you create a model from 2 to 5-2:
Figure 2-5-2
The preceding model contains two navigation attributes: PictureCategory1 and PictureCategory2.
Cause:
The features of the database relationship include degree, multiple features, and directionality.
Degree is the number of entity types involved in the link. The relationship between one dollar and two yuan is the most common, and the relationship between three yuan and N yuan is the theoretical significance of multi-language.
Multiple is the number of terminals of each object type in the link. There are 1-to-many, 1-to-1.
Directionality refers to unidirectional or bidirectional data.
The Entity Data Model of EF supports a special database relationship called Assioation ). In the relationship of the association type, the degree is one or two yuan, and the multiple degrees are 0 .. 1 (0 or 1), or *. The direction is bidirectional.
In this example, the degree is mona1 (only the physical type of PictureCategory), the multiple degrees are 0 .. 1 (0 or 1) and *, and the direction is also bidirectional.
The self-reference type usually has a parent-child relationship. Each parent can have many children, but one child has only one father. Because the parent is 0 .. 1 (0 or 1), the child can have no father. This represents your root node, that is, the node with only children without fathers.
The following code recursively enumerates all picture categories from the root node
static void RunExample() { using (var context = new EFRecipesEntities()) { var louvre = new PictureCategory { Name = "Louvre" }; var child = new PictureCategory { Name = "Egyptian Antiquites" }; louvre.Subcategories.Add(child); child = new PictureCategory { Name = "Sculptures" }; louvre.Subcategories.Add(child); child = new PictureCategory { Name = "Paintings" }; louvre.Subcategories.Add(child); var paris = new PictureCategory { Name = "Paris" }; paris.Subcategories.Add(louvre); var vacation = new PictureCategory { Name = "Summer Vacation" }; vacation.Subcategories.Add(paris); context.PictureCategories.AddObject(paris); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { PictureCategory root = (from c in context.PictureCategories where c.ParentCategory == null select c).FirstOrDefault(); Print(root, 0); } } static void Print(PictureCategory cat, int level) { StringBuilder sb = new StringBuilder(); Console.WriteLine("{0}{1}", sb.Append(' ', level).ToString(), cat.Name); foreach (PictureCategory child in cat.Subcategories) { Print(child, level + 1); } }
Output
-------------------------------------------Summer VacationParisLouvreEgyptian AntiquitesSculpturesPaintings-------------------------------------------
Now let's look at the code. First, we create and instantiate our object type. By adding the created sub-PictureCategory to the louvre class, we will connect the class diagrams. Add the louvre class to the paris class. Finally, add the paris class to the summer vacation class. In this way, the hierarchy is built from the bottom to the top.
Once we call SaveChanges () to zoom in, all these will be inserted into the database. Then we can perform the query operation to see which records are successfully inserted.
In the read part, we start with the root object. This is an instance without a parent node, that is, our summer vacation. There is a root node. We can call the Print () method written by ourselves. The Print () method has two parameters: one is the PictureCategory instance, and the other is the level, or the depth in the hierarchy. Because the summer vacation is the root node, its depth is 0. In the Print method, Print (root, 0) should be used ).
In the Print method, we write the category name and add a space in front to indicate its depth. The Append () method generates spaces to indicate depth. In the recursion part, we call the Print () method recursively to traverse all the subnodes. At the same time, make sure that level needs to be increased by 1 for each layer. when all the sub-nodes have been traversed, release it. The result is as shown above.
In section 6-5, we will introduce another method to solve this problem. In the stored procedure, we use the Common Table Expression to traverse the entire class graph, and then return a two-dimensional result set.