About this Book

Entity Framework is the Microsoft-recommended tool to read and persist data inside a relational database. With this software, Microsoft has entered the O/RM market with a reliable product that significantly eases data access development.

This book will take you from the apprentice to the master level in the Entity Framework technology. You can think of this book as a guided tour through Entity Framework features and best practices. When you have finished reading Entity Framework 4 in Action, you’ll be able to confidently design, develop, and deliver applications that rely on Entity Framework to persist business data.

Who Should Read this Book?

This book was written for all Entity Framework developers, whether you develop small home applications or the largest enterprise systems. Everything from home DVD library applications to e-commerce solutions that interact with many heterogeneous systems and store lots of information can benefit from Entity Framework, and this book will show you how.

Roadmap

This book will walk you through the creation of an application from scratch, and will show you how to keep improving it with various Entity Framework features. This Entity Framework tour will cover all of Framework’s features over the course of nineteen chapters, grouped in four parts.

In part 1 we introduce the basics of the O/RM pattern and show you the fundamentals of Entity Framework as we create the foundation for an application.

Chapter 1 provides a high-level overview of the O/RM pattern and of the Entity Framework components. By the end of this chapter, you’ll understand why O/RM tools are so useful and how Entity Framework accomplishes its tasks.

Chapter 2 shows how you can create an application from scratch and how to persist objects in the database. First, you’ll learn two ways of designing an application using Entity Framework. Then, after the application is created, you’ll learn how to read, manipulate, and persist data. By the end of this chapter, you’ll have a clear understanding of the advantages of adopting Entity Framework.

In part 2 of the book, we discuss the main building blocks of Entity Framework in detail: mapping, querying, and persistence.

Chapter 3 covers the basics of querying. Here you’ll learn about the main component that enables Entity Framework to work with objects. You’ll also discover how Entity Framework enables you to write queries against your model that will successfully hit the database.

Chapter 4 focuses on querying with LINQ to Entities. In this chapter, you’ll learn how to filter, group, project, and join data using the main query language of Entity Framework. By the end of this chapter, you’ll be able to perform any type of query.

Chapter 5 discusses mapping between entities in the model and the database. Here you’ll learn how to accomplish this visually with the designer, but you’ll also learn how to manually modify the mapping file. By the end of this chapter, you’ll have a full knowledge of the mapping mechanism in Entity Framework.

Chapter 6 tours the entity lifecycle. You’ll learn how Entity Framework treats entities, what state an entity can be in, how to modify the state, and how state affects an entity’s persistence. By the end of the chapter, you’ll be able to write code that prepares your objects for persistence into the database.

Chapter 7 discusses persisting objects into the database. In chapter 6 you learned how to prepare entities for persistence; here you’ll learn how to actually save them. This subject has many intricacies and pitfalls, especially where related entities are involved. This chapter focuses on these potential problems so that you can understand and avoid them. By the end of the chapter, you’ll be able to persist any entity in any way you need.

Chapter 8 covers Entity Framework’s concurrency and transaction features. In the first part of the chapter, you’ll be introduced to the concept of concurrency and what problems it solves when data is saved to the database. Then you’ll learn how Entity Framework lets you easily manage concurrency. Finally, you’ll learn how Entity Framework manages transactions to persist multiple entities, and how you can extend a transaction’s lifetime to execute custom commands.

Part 3 of the book will show you how to take advantage of Entity Framework’s most advanced features.

Chapter 9 introduces Entity SQL. Entity SQL is Entity Framework’s other querying language, and it’s still the most powerful (although less appealing than LINQ to Entities).

In this chapter, we’ll take the LINQ to Entities examples from chapter 4 and rewrite them in Entity SQL. You can see them side by side and choose the approach that is easier for you. By the end of this chapter, you’ll have a full knowledge of all the querying techniques Entity Framework offers.

Chapter 10 covers stored procedures. Here you’ll learn how to have Entity Framework call stored procedures to query and update entities instead of having it generate SQL for you. By the end of this chapter, you’ll be able to create your own set of stored procedures and have Entity Framework invoke them, so that your DBA is happy.

Chapter 11 discusses views and functions embedded in mapping. You’ll see how to create internal views that can be queried easily, and how to create functions that can be reused when querying with both LINQ to Entities and Entity SQL. By the end of this chapter, you’ll be able to write queries that are easy to maintain and reusable.

Chapter 12 discusses how to retrieve mapping information. Chapter 5 explains how to map your model classes to database tables and views; in this chapter you’ll learn how to retrieve this mapping information. You’ll also see some real-world examples that will demonstrate why this technique is valuable. After finishing this chapter, you’ll be able to write powerful generic code that takes data from mapping files.

Chapter 13 covers code generation. Here you’ll discover how Entity Framework is integrated with Visual Studio, and how this integration lets you create code and even generate database scripts starting with mapping information. You’ll also discover how to customize the Entity Framework designer inside Visual Studio. After you’ve finished this chapter, you’ll be able to fully customize the designer, adding behaviors that simplify development.

In part 4 of the book, we’ll show you how to best use Entity Framework with different types of applications: Windows, web, and web services applications.

Chapter 14 discusses application design. You’ll learn about the classic three-layer pattern and then go on to the Domain Model pattern. Finally, you’ll read about the famous Repository pattern, and learn why it’s a great choice for many applications. By the end of this chapter, you’ll be able to create a well-designed and layered application.

Chapter 15 explains how to integrate Entity Framework into ASP.NET applications. In this chapter, you’ll read about ASP.NET controls and about best practices for handling objects. This will enable you to create web applications using the correct patterns.

Chapter 16 discusses how to create web service applications. Here you’ll learn about specific features dedicated to the web service environment and how and when to use them instead of resorting to other techniques. By the end of this chapter, you’ll have a strong understanding of web services and Entity Framework integration.

Chapter 17 explains how to integrate Entity Framework into Windows applications. Here you’ll discover how to let your model classes implement a specific interface so that they are integrated with the data-binding capabilities of Windows Form and WPF applications. By the end of this chapter, you’ll be able to face everyday problems involving these types of applications.

Chapter 18 covers testing. Here you’ll learn how to test the code that accesses the database and your repositories, and how to create batteries of tests to reveal if your modifications have broken something.

Chapter 19 discusses performance. You’ll learn how Entity Framework performance compares to performance in the classic ADO.NET approach. You’ll also learn some tricks and tips to improve performance in various situations. By the end of the chapter, you’ll be able to boost the performance of your data access code to the edge.

Appendix A introduces LINQ. LINQ to Entities is the most popular querying language for Entity Framework. It’s a dialect of LINQ, so to better understand it you should have a good knowledge of LINQ. That’s what this appendix offers.

Appendix B presents some good Entity Framework tips. You won’t learn about new features here, but you’ll learn how to merge existing features to produce powerful behaviors. This is your ultimate resource in understanding how much power Entity Framework hands you.

Code Conventions

All source code in listings or set off from the text is in a fixed-width font like this to separate it from ordinary text. The .NET code is provided in both C# and Visual Basic so that you should be comfortable with it, whatever your development language. For longer lines of code that won’t fit on the page, a code-continuation character () is used to indicate lines that are broken on the page but shouldn’t be broken in the code. Code annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

Source Code Downloads

All the examples in this book can be downloaded from http://www.entityframeworkinaction.com/download.aspx or from the publisher’s website at www.manning.com/EntityFramework4inAction. The code comes in both VB and C# versions.

The code comes with a Visual Studio 2010 solution file so you only need Visual Studio 2010 to run the examples. We did not try to open the solution file with Visual Studio Express 2010 (that is the free version), but it’s likely to work.

Author Online

The purchase of Entity Framework 4 in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and other users. You can access and subscribe to the forum at http://www.manning.com/EntityFramework4inAction. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the authors can take place. It isn’t a commitment to any specific amount of participation on the part of the authors, whose contributions to the book’s forum remain voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

In addition to the Author Online forum available on Manning’s website, you may also contact us about this book, or anything else, through one of the following avenues:

All comments sent to these blogs are moderated. We post most of the comments, but if you include your email address or phone number, we won’t post the comment out of respect for your privacy.

About the Authors

STEFANO MOSTARDA is a Microsoft MVP in the Data Platform category. He’s a software architect mainly focused on web applications, and is a cofounder of 5DLabs.it, a consulting agency specializing in ASP.NET, Silverlight, Windows Phone 7, and the .NET Framework. Stefano is a professional speaker at many Italian conferences on Microsoft technologies and he’s a well-known author. He has written many books for the Italian market and is a coauthor of Manning’s ASP.NET 4.0 in Practice. He’s one of the leaders of the ASPItalia.com Network and a content manager of the LINQNItalia.com website dedicated to LINQ and Entity Framework. You can read his technical deliriums both on his blog and on Twitter at http://twitter.com/sm15455/.

MARCO DE SANCTIS has been designing and developing enterprise applications in distributed scenarios for the last seven years. He started developing with ASP.NET as soon as it came out, and since then has become an application architect. Through the years he specialized in building distributed services, widening his knowledge to encompass technologies like Workflow Foundation, Windows Communication Foundation, LINQ, and ADO.NET Entity Framework. Today he works as a senior software engineer for major Italian companies in the IT market. In his spare time, he’s a content manager at ASPItalia.com and has recently been named a Microsoft Most Valuable Professional in ASP.NET. You can read his thoughts on twitter at http://twitter.com/crad77.

DANIELE BOCHICCHIO is a cofounder of 5DLabs.it, a consulting agency specializing in ASP.NET, Silverlight, Windows Phone 7, and the .NET Framework. He has worked on a lot of cool projects with many different technologies. Daniele is a well-known professional speaker and author, and you can find him at developer-focused events worldwide. He has written several books, both in Italian and English, including ASP.NET 4.0 in Practice, published by Manning. He is also the network manager of the ASPItalia.com Network, the largest Italian .NET Framework community. Daniele’s personal website is located at http://www.bochicchio.com/ and he shares his thoughts in 140 chars or less at http://twitter.com/dbochicchio/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.97.170