Entity Framework 6 Recipes Chinese translation series (40), entityframework
For the original intention of translation and why I chose Entity Framework 6 Recipes, see Chapter 7-5 of this series to obtain entities from the tracker.
Problem
You want to create an extension method to obtain objects from the tracker and perform some operations before saving the data.
Solution
Suppose you have a model 7-7.
Figure 7-7. Model Containing entity Technician and ServiceCall
In this model, each technician (technician) has some service calls, which contain the contact name and question. Use code listing 7-4 to create an extension method to retrieve all entities whose state is Added, Modifed, or Unchanged.
Code List 7-4.Create an extension method to obtain all entities whose state is Added, Modifed, or Unchanged
1 public static class Recipe5Program 2 {3 public static void Run () 4 {5 using (var context = new Recipe5Context ()) 6 {7 var tech1 = new Technician {Name = "Julie Kerns"}; 8 var tech2 = new Technician {Name = "Robert Allison"}; 9 context. servicecils. add (new ServiceCall10 {11 ContactName = "Robin Rosen", 12 Issue = "Can't get satellite signal. ", 13 Technician = tech114}); 15 context. serviceCall S. add (new ServiceCall16 {17 ContactName = "Phillip Marlowe", 18 Issue = "Channel not available", 19 Technician = tech220 }); 21 22 // obtain the entity 23 foreach (var tech in24 context. changeTracker. getEntities <Technician> () 25 {26 Console. writeLine ("Technician: {0}", tech. name); 27 foreach (var call in tech. servicecils) 28 {29 Console. writeLine ("\ tService Call: Contact {0} about {1}", 30 call. contactName, c All. issue); 31} 32} 33} 34 35} 36} 37 public static class ChangeTrackerExtensions38 {39 public static IEnumerable <T> GetEntities <T> (this DbChangeTracker tracker) 40 {41 var entities = tracker42. entries () 43. where (entry => entry. state! = EntityState. Detached & entry. Entity! = Null) 44. Select (entry => entry. Entity). OfType <T> (); 45 return entities; 46} 47}
The output of code list 7-4 is as follows:
Technician: Julie KernsService Call: Contact Robin Rosen about Can't get satellite signal.Technician: Robert AllisonService Call: Contact Phillip Marlowe about Channel not available
Principle
In code listing 7-4, we implemented the extension method GetEntities <T> (), which gets all entities in the upper and lower states as Added, Modified, and Unchanged. This is a common method, so there is a reason to implement it only once. In the implementation of the GetEntities <T> method, we use LINQ-to Entities to filter the entire set of Entries <T> () methods. This method returns all non-Detached entries. We filter out links and null entries from the returned results, and select only the specified type of entries from the remaining entries.
In many important scenarios, You need to implement methods similar to the GetEntities <T> () method. For example, in a SaveChanges event, you want to verify the entity to be inserted, modified, or deleted.
7-6 generate a model from the command line
Problem
You want to generate a model from the command line.
Solution
Use the edmgen.exe program to generate a model from a given database. In the Start Menu, click Visual Studio 2012 Command Prompt (Command Prompt) under Microsoft Visual Studio2012 to access the Visual Studio 2012 Command Prompt tool.
Microsoft official documentation provides a complete command line option description for edmgen commands. The edmgen command provides many useful command line options. For example, the following command generates a model from all tables in the test database.
edmgen /mode:FullGeneration /project:Test /provider:"System.Data.SqlClient" /c:"server=localhost;integrated security=true;database=Test;"
Other/model options are also available. One of them is useful in the continuous build process. It is/mode: ValidateArtifacts. With this option, one or more verified layers can be generated. You may use one or all of the following options/inssdl or/incsdl. If you want to verify the ing layer, the three layers in the model must be specified.
When generating a file for the specified model layer, you can use the/out option to name the file. If/outcsdl: MyProject. csdl is used, a file containing the definition of the concept layer will be created, with the file name MyProject. csdl. The usage of other layers is similar to this.
Principle
The edmgen command provides a convenient way to automatically build a model. It is also a useful tool for pre-generating query views and generating independent files for each model layer. One restriction of edmgen is that it cannot generate a database table-based Subset model.
Using the edmgen command to pre-generate a view can greatly improve the performance of an application. When a query is executed, the Entity Framework must construct a series of views for accessing and querying the database. The edmgen utility is not used. view generation is performed when the first object framework is called. If the data model is small, the first initialization will not pose much risk. However, if the data model is very large or complex, such performance impact is unacceptable. In this case, we have enough reason to use the edmgen command line entity tool..
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/