Chapter 12 in Pro LINQ Object Relational Mapping with C #2008
Vijay P. MehtaAPress
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. 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.
You have learned about LTS and EF. As a. Net developer, you should now look at the advantages and disadvantages of these two tools and compare them with other ORM tools. In this chapter, we will compare the two tools and introduce other. Net ORM tools on the market.
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 stronugly typed LINQ access for rapidly developed applications within ss the Microsoft SQL Server family of databases."
Lts is suitable for quick development of SQL Server databases. Microsoft recommends and proves this in practice. Lts supports direct database-to-class ing, which has some functional limitations. According to msdn, lts is designed to use the SQL Server family for rapid development.
Alternatively, msdn classifies EF as "designed to provide stronugly typed LINQ access for applications requiring a more flexible object-relational mapping, within SS Microsoft SQL Server and third-party databases. "Additionally, I classify EF as the enterprise solution because it separates its metadata into well-defined tural 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 extends other constructs (for example, esql and provider independence) as well as LINQ to entities.
For EF, msdn defines EF as "designed for SQL Server and other databases and requires strong ORM-type LINQ with more powerful functions ". In addition, I positioned EF as an enterprise-level solution because EF can well define metadata in layers. It loosely coupled and organized the concept and physical model division. Lts can only use LINQ to SQL, but EF provides more structures such as esql, LINQ to entities, and other independent LINQ providers.
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.
The best way to analyze the differences between the two tools is to look at their functional support. The rest of this chapter introduces the conclusions you can see in Chapter 11. Compare these products so that you can achieve the best results when using these tools.
Note Although EF provides varous techniques for manipulating data, the following analysis
Focuses on EF as a whole, with special attention given to LINQ to Entities.
Although EF provides a variety of techniques for operating data, the following analysis focuses on the LINQ to Entities.
Functionality Differences LTS is geared specifically at direct 1:1 mapping. 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 stronugly 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.
The difference between the LTS function is that the 1:1 ing between the SQL Server database table and the. Net class is 1:1. LTS is designed for rapid development, when your database
Most of the ing is in the domain model (each class corresponds to a table), and the foreign key is associated with a strong type relationship. According to MSDN, LTS APIs is
Designed for general application scenarios.
In this chapter, MSDN references can be found at http://msdn2.microsoft.com/en-us/library/cc164244.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 writable 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.
The Simplified Design of LTS is not only reflected in the APIs and DML used for CRUD (Create Read Update Delete), but also in the settings of VS2008
It is also simpler than EF. In Figure 12-1, the simple LTS design interface only provides basic ORM functions. Many ORM solutions provide rich attributes and options. We can see that the LTS design interface can be quickly designed.
Figure 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.
Using this design interface, LTS also provides table support for inheritance (ORM is a very direct inheritance form ). In Figure 12-2, the interface learned in the previous book allows you to use the column identifier in your database to design inheritance.
Figure 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 your enterprise environments because of its limited out-of-the-box support for complex scenarios.
It is important that the database and entity for simple applications only have a 1:1 ing of. LTS is an ideal tool. However, LTS is not suitable for complex enterprise-level application scenarios that require "out-of-the-box deployment.
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. database tables.
Unlike LTS, EF is designed for large-scale enterprise-level applications and databases are managed by DBAs. EF uses a complex ing algorithm to divide metadata into concepts, storage, and ing. It allows you to design domain-based entities on database tables.
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. edmx file.
For example, you can use EF to map an object to multiple tables or views, or map multiple objects to one table or view. Figure 12-3 shows EF's support for this complex scenario. As you can see in the previous chapters, you can use the visual designer to edit the. edmx file. EF can define more attributes than 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.
Another important feature of EF as an enterprise-level solution is its support for inheritance. LTS only supports Simple inheritance. All Derived classes correspond to one Table (Table per Hierarchy ). EF supports three core ORM inheritance modes: All Derived classes correspond to one Table (Table per Hierarchy), and each derived Class has one Table (Table per Class ), each specific class is a table (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.
Figure 12-4 shows more powerful functions of EF than LTS. In this example, you can use a ing browser to manage different inheritance of the model and control ing between object classes and tables and views.
Another reason why EF has rock star status is because of its support for complex composite types and has-to-have 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.
More importantly, EF also supports complex combinations of types and many-to-many relationships. In addition to Linq to Entities, EF can also use Entity SQL and strong type query providers, similar to the design in LTS. EF can use different providers, and LTS cannot.
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.
Although LTS Applications lack some features in complex scenarios, you can easily use LTS in enterprise applications by using the models and practices in this book.
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.