LINQ to SQL(LTS) vs Entity Framework(EF)

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.

 

posted on 2009-08-30 22:09  Utopia Coming  阅读(1208)  评论(0编辑  收藏  举报

导航