The original series is from English.
Tip 1: How to sort the relationship (Relationships) in Entity Framework)
Problem:
In the Entity Framework forum, you will often see questions about sorting associated projects.
For example, imagine you want to query the customer and return the accounts that owe more than 30 yuan, while searching the orders of these accounts.
And you need to sort those orders by order date, so that you can first see the most recent order, so that you can easily find suspicious behavior.
Answer:
Most people may know that EF can use include () to load a link instantly. Example:
VaRLatecustomers =FromCInCTX. MERs. Include ("Orders")
WhereC. ismorethan30daysinarrears
SelectC;
Unfortunately, the orders of each customer returned by this statement are unordered.
So how do you sort these orders? Strictly speaking, there is no way.
But you can also do some such work, such as the include operation in the select clause.
VaR Latecustomers =From C In CTX. MERs
Where C. ismorethan30daysinarrears
Select New {
Customer = C,
Orders = C. Orders. orderbydescending (
O => O. orderdate
)
};
This statement uses the standard LINQ syntax to request an anonymous iteration consisting of the customer and all the orders sorted by it.
Entity Framework can support this method, so the problem is solved...
Additional knowledge:
The above practices are somewhat...
Instead of using entities to retrieve data, I used other things-An anonymous type.
This method is not ideal.
You really want to access the customer's order through the customer. Orders attribute. It is interesting that even if something named fix-up has achieved this goal.
The object service of Entity Framework automatically binds related items together. This process is called fix-up.
Generally, the fix-up process occurs when two associated entities enter the context.
So because I have loaded both customer and orders (via projection), the fix-up will also ensure that the customer. Orders attribute contains those orders. This is the real reason why include ("orders") is not used in my query.
This avoids the problem and leads to a new problem: Can customer. Orders be sorted?
Unfortunately, there is no official solution.
This is not a feature we want to support. We don't have to spend time-we need to test it to make sure it works in every scenario. You know that is an important QA issue.
Although there are opponents, this can work in any way...
Why?
My guess is to execute sorting in the query, write an object based on datareader and finally fix-up.CodeThere are side effects.
Now, theoretically you can use this feature in the project... However, note that this is a big turning point. Please understand that this is not recommended.
Depending on a special implementation with potential negative effects, there is always a risk.
Tip 2. Entity Framework books
Problem:
Where can I find a book to learn more about Entity Framework?
Answer:
One advantage of developing products in the Microsoft product team is that you can access the author who wants you to review their books.
So far, I have received three copies of their Entity Framework books from different authors.
As follows:
ADO. NET Entity Framework-Unai zorrilla Castro/Octavio Hernandez/Eduardo Quintas
This book is written in Spanish and is essentially a single language, so it is difficult for individuals to comment on its quality. However, it is enough for me to recommend this book by my colleagues in Argentina. Even so, I still like to put it on my bookshelf, which makes me very advanced... So thank you, Unai.
Programming Entity Framework-Julie Lerman
I have met Julie several times and I know that she has devoted her painstaking efforts to it. So it is not surprising that this nearly 800-page book is so thorough. Julie's book contains a lot of useful information and some practical examples.
Julie, thank you for your book.
Professional ADO. Net 3.5 with LINQ and the Entity Framework-Roger Jennings
I have never met Roger, but he knows from his blog that he makes us feel reliable. His book is a little different from Julie's book because it tells about other things around entitiy framework, such as LINQ to XML and LINQ to dataset. I just received this book yesterday (Thank you Roger), but I believe it is very market-oriented.
As you can see here, these are good choices...
Wish your Entity Framework a smooth journey
Tip 3. Start a tour of T4
If you have readEntity Framework design blogYou should have heard about it.T4. This is a technology released together with Visual Studio 2008 (2005 has an independent version for download ).
In. net4.0, Entity Framework uses T4 to enhance code generation and model initialization.
In fact, T4 is also used in other major Microsoft products, including ASP. net mvc and dynamic data.
So if you want to start using T4 and get familiar with this technology, what should you do?
In fact, this technology is relatively simple. You can easily accomplish something quite useful:
-
- Add a text file to the project and change the extension to ". tt ".
- write some template code in a text file.
<# @ import namespace =" system. collections. generic "#>
<#
dictionary properties = new dictionary ();
properties. add ("Age", typeof (INT);
properties. add ("firstname", typeof (string);
properties. add ("surname", typeof (string);
#>
using system;
public class <# =" myclass "#>{< br> <# foreach (string name in properties. keys) {#>
Public <# = properties [name]. name #><#= name #>{
get; set;
}< BR >##>< BR >}
These template codes generate a class and use each key in the dictionary named "properties" as its attribute.
As you can see, T4 is very simple and easy to use for users familiar with ASP. NET...
Give it a try.
Tip 4: define language rules in conceptual Mode
The Entity Framework of the first version was released with. Net 3.5 SP1 not long ago.
One of the biggest vulnerabilities in the Entity Framework documentation is the lack of a formal description of the CSDL (concept mode Description Language.
What people want to know about CSDL is the specific format used by Entity Framework to describe the Entity Data Model (EDM.
These rules can be embeddedSystem. Data. entity. dllProgramThe XSD in the set resource is pushed out, but a large number of rules are restricted by the Entity Framework itself.
If you are interested in the details of these rules, seeFormal CSDL specification here.
This document focuses on the csdl1.1 version used by Astoria, but most of the csdl1.0 and 1.1 versions are the same. These differences are clearly listed here.
Disclaimer: I helped edit some parts of this document. If you have any problems, please let me know.
Tip 5: How to restrict the types returned by EF queries
Consider that you have a model like this:
How to query only cars?
This is when oftype <subtype> () was launched. You can write the following code:
VaROnlycars =FromCarInCTX. Vehicles. oftype <Car> ()
SelectCAR;
And this can work well. This limits the results to cars, with cars and sports included.
How to query only cars without its subtype?
Consider that your budget is enough to select a simple household car. This means that you do not want an SUV or a sports car (sportscars ).
You need to specify the limit in the query to avoid returning all child types:
VaROnlycars =FromCarInCTX. Vehicles. oftype <Car> ()
Where! (CarIs Sportscar)&&! (CarIs SUV)
SelectCAR;
Now, your code only returns cars-type objects.
Upload
The only pity about this solution is that you have to display and exclude all child types that you might not want. In some cases, there may be many sub-classes with multi-level inheritance or a large number of derived classes at a level.
It would be better if the following statement is supported (unfortunately this is not supported ):
VaROnlycars =FromCarInCTX. Vehicles. oftype <Car> ()
WhereCar. GetType () =Typeof(Car)
SelectCAR;
The problem is, How important do you think it is to support this type of query?
BTW: C # Why does the data structure and algorithm series translation stop .. this book already has a Chinese version (for details) for interviews. I will not repeat it ..