Chapter 12 in Pro LINQ Object Relational Mapping with C# 2008
Vijay P. Mehta APress
LINQ to SQL (LTS) vs the Entity Framework(EF)
Now that you have seen LTS and EF in action, it is time to look at the strengths and weaknesses of each of the tools, along with other ORM options that you have available to you as a .NET developer. This chapter examines some of the differences between the two ORM tools, the lessons I’ve learned while writing this book, and other .NET ORM tools that are available on the market.
你已經瞭解了LTS 和 EF,作為一個.Net開發人員,現在應該看看這兩種工具的優缺點,比較一下其他ORM工具。這章我們比較一下這兩種工具並介紹一下市場上其他的.Net ORM工具。
LTS vs. EF
The general consensus when it comes to LTS is that it is best suited for rapid application development specifically against a Microsoft SQL Server database. This message is both espoused by Microsoft and is evident in the design of the software. For example, LTS really supports only a direct mapping of your database schema to your classes and is limited in some of its functionality. According to MSDN, LTS is “designed to provide strongly typed LINQ access for rapidly developed applications across the Microsoft SQL Server family of databases.”
LTS比較適合使用SQL Server資料庫的快速開發。微軟推薦並且在實踐中也證明了這一點。LTS支援直接的資料庫到類的映射,在功能上有些限制。根據MSDN,LTS被設計為使用SQL Server家族產品進行快速開發。
Alternatively, MSDN classifies EF as “designed to provide strongly typed LINQ access for applications requiring a more flexible object-relational mapping, across Microsoft SQL Server and third-party databases.” Additionally, I classify EF as the enterprise solution because it separates its metadata into well-defined architectural layers. These layers are well suited for a large enterprise because they provide you with loose coupling and fine-grained control over the conceptual and physical models. In addition, whereas LTS is designed only as LINQ to SQL,EF presents many other constructs (for example, ESQL and provider independence) as well as LINQ to Entities.
對於EF,MSDN對EF的定義是“被設計用於SQL Server和其他資料庫,需要功能更加強大的ORM的強型別LINQ”。另外我將EF定位為企業級解決方案,因為EF可以將中繼資料非常好的分層定義。它將概念和物理模型劃分的松耦合且條理清晰。LTS只能使用LINQ TO SQL,但是EF提供了更多的結構比如ESQL,LINQ TO Entities和其他獨立的Linq Provider。
The best way to examine the two products is to look at some of the differences in the functionality that they support. Much of the information in the rest of this section will be conclusions that you may have already drawn from reading the previous 11 chapters; however, along with comparing the products, the goal is also to present you with a list of best practices on when to use which tool.
分析兩種工具的差別最好的方法就是看他們在功能上的支援。本章剩餘部分介紹你從11章看到的一下結論。比較這些產品以便你在使用這些工具時可以達到最好的效果。
Note Although EF provides various techniques for manipulating data, the following analysis
focuses on EF as a whole, with special attention given to LINQ to Entities.
雖然EF提供多種操作資料的技術,下面的分析主要關注LINQ to Entities。
Functionality Differences LTS is geared specifically at direct 1:1 mapping of .NET classes to SQL Server database tables.The goal in the design of LTS is clearly rapid development, where your database for the most part is mirrored in your domain model (that is, Table per Class), and foreign keys are exposed as strongly typed relationships. According to MSDN, the LTS APIs have been designed to “just work for common application scenarios.”
Note All of the MSDN references in this chapter can be found at http://msdn2.microsoft.com/en-us/library/cc161164.aspx.
LTS功能的不同是受SQL Server資料庫表與.Net類的1:1映射關聯。LTS設計用於快速開發,當你的資料庫
的大部分映射在領域模型上即(每個類對應一張表),並且外鍵以強型別關係關聯。根據MSDN,LTS APIs被
設計用於一般的應用情境。
本章中MSDN引用可以在http://msdn2.microsoft.com/en-us/library/cc161164.aspx找到。
The simple design of LTS is evident not only in the API and the DML that gets generated for your CRUD operations, but also in the UI designer integrated in VS 2008. In Figure 12-1, you can see the simple design of the UI, which provides you with rudimentary functionality to manipulate your ORM solution. Whereas many ORM solutions provide you with a plethora of properties and options, LTS as seen in Figure 12-1 gives you the path of least resistance and handles nearly everything for you behind the scenes.
LTS的簡化設計不僅體現在做CRUD(Create Read Update Delete)時使用的API和DML,在VS2008的設
計器也較EF簡化。在圖12-1,簡單LTS設計介面僅提供了ORM基本的功能。很多ORM解決方案都提供很豐富的屬性和選項。可以看出LTS設計介面可以很快速的設計。
圖 12-1
Along with the simple UI, LTS also offers Table per Hierarchy inheritance, the most straightforward form of inheritance in ORM. As you learned earlier in the book, this form of inheritance is supported through the use of a discriminator column in your database as well as the UI, as seen in Figure 12-2.
使用這個設計介面,LTS同樣提供表對繼承的支援(ORM非常直接的繼承形式)。在圖12-2,在之前本書中學習到的這個介面支援在你的資料庫使用列辨識器來設計繼承。
圖12-2
Another important point about LTS that further supports the assertion that LTS is ideal for simple applications is that the mapping metadata is limited to 1:1 direct relationships between your entities and your database. This excludes LTS from many enterprise environments because of its limited out-of-the-box support for complex scenarios.
重要的是對於簡單的應用它的資料庫和實體僅是1:1的映射,LTS是個非常理想的工具。但是對於複雜的企業級應用情境需要“開包即用”採用LTS就不合適。
Unlike LTS, EF is designed specifically for large enterprise applications, where a DBA team is in control of your database schema. EF uses a complex mapping algorithm to separate the metadata into layers (that is, Conceptual, Storage, Mapping), which enables you to model your domain based on entities vs. database tables.
不像LTS,EF轉為大型企業級應用設計,資料庫是由DBA來管理。EF使用複雜映射演算法將中繼資料分成概念、儲存,映射。他讓你可以在資料庫表之上作基於域的實體設計。
For example, in EF you can map a single entity to one or more tables or views, or you can map multiple entities to the same table or view. This support for complex scenarios is exemplified by the EF user interface seen in Figure 12-3. Here, as you have seen in earlier chapters, you have a more defined properties window than in LTS and you have the Model Browser window, which gives you UI access to the .edmx file.
例如,使用EF你可以將一個實體映射到多個表或視圖,或者將多個實體映射到一張表或視圖。在圖12-3你可以看出EF對這種複雜情境的支援。如你在前幾章看到的,使用可視化的設計器編輯.edmx檔案,EF可以比LTS定義更多的屬性。
Another aspect that further demonstrates EF’s ability to be an enterprise solution is its support for inheritance. Unlike LTS, which supports simple inheritance only in the form of Table per Hierarchy, EF supports all the core ORM inheritance patterns. These patterns include Table per Hierarchy, Table per Class, and Table per Concrete Class.
EF作為一個企業級的解決方案另一個重要的特點是它支援繼承。LTS僅支援簡單繼承,所有衍生類別對應一張表(Table per Hierarchy)。EF支援三種的核心ORM繼承模式:所有衍生類別對應一張表(Table per Hierarchy),每個衍生類別一張表(Table per Class),每個具體類一張表(table per concrete class) 。
Figure 12-4 shows additional UI functionality, which is another differentiator for EF. In this example, you have the Mapping Details browser, which enables you to manipulate different forms of inheritance in your model and also enables you to control the mapping of your entities to tables and views.
圖12-4展示了EF比LTS更強大的功能。在這個例子,你使用映射瀏覽器管理模型的不同繼承,同時可以控制實體類到表和視圖的映射。
Another reason why EF has rock star status is because of its support for complex composite types and many-to-many relationships. Additionally, where EF has LINQ to Entities, which is similar in design to LTS, EF also has Entity SQL and a strong provider model. Additionally, EF is approaching provider independence and has overall momentum in the community, things that LTS is missing.
使得EF更加重要的是它還支援複雜的組合類別型和多對多的關係。另外EF除了有Linq to Entities,類似在LTS中的設計,EF還可以使用Entity SQL 和強型別查詢Provider。EF可以使用不同的Provider,LTS是不能的。
Although LTS lacks some functionality for more complex scenarios, by using the patterns and practices learned in this text, you can easily adapt the use of LTS to any enterprise application.
雖然LTS應用在複雜的情境中缺少一些功能,但是使用本書中的模式和實踐,你可以輕鬆的在公司專屬應用程式中使用LTS。
However, if you are looking for an ORM option that has everything you need out of the box for a complex n-tier application, you may want to start with EF.My general opinion after writing this book is that both solutions have flaws. This has to be expected as these are, after all, Microsoft’s first real go at ORM. I don’t plan on dropping my third-party ORM tools from my solutions anytime soon; however, as Microsoft’s products mature and as the community embraces ORM, I will likely be making the switch sometime in the future.