The database used for this study is as follows
1. What is Dto:datatransferobject
That is, the data transfer object, the client communication on the server, automatically defines a small entity class that contains only the attributes we need to transfer |
2. What's wrong with a DTO?
A. When using JavaScriptSerializer, an error will be made.
Cause: The teacher entity has a foreign key entity Teachclass, and Teachclass has a teacher collection property, and then it loops the query, and there is a dead loop
B. Error when using JSON to return data
3. Using DTOs to solve problems
Now the question:
When serializing the teacher object, the foreign key entity Teachclass is found, and when the Teachclass object is found, the teacher is re-searched until the loop. |
Start the solution:
A. Defining a DTO class to resolve a dead loop
We can define a new Teachclass class that does not contain properties that cause a dead loop, that is, no icollection<teacher> property. Here we also re-declare a class for teacher.
B. Defining partial classes for easy addition of additional methods
The death cycle problem is solved, this is the DTO, simple. Next, we may want to use serialization in many places, so we define a method in the inside of the class and serialize ourselves for convenience. But again, because the entity class is generated by EF, each time we click Save, EF initializes the class according to the TT template and the XML configuration information, and the method we wrote is written in White. This time, we think of a partial class, a method that serializes the class in a partial class.
There is an idea because EF is looking for a class by file name, so the class name here cannot be the same as the class name
C. Start using your own defined DTO class
found that the download program is working properly, did not report any exceptions, done
MVC Series Learning (ix) use of-dto