Preface

Anyone who has been developing on the Microsoft platform for the last several years knows the drill: there’s a new database access technology every few years. There was ODBC, then DAO and RDO, OLEDB, ADO, and ADO.NET, LINQ to SQL, and now Entity Framework! Although this progression of technologies has introduced rapid change, it has also been wonderfully refreshing as we have evolved from simple open connectivity, to componentized connectivity, to disconnected access in a managed environment, to frictionless access syntax, and finally to conceptual modeling.

Conceptual modeling is the defining feature of Entity Framework, and it is at the heart of this book. Entity Framework builds upon the previous data access paradigms, and it provides an environment that supports rich, real-world domain-level modeling. We can now think of and program against real-world items, such as orders and customers, and leverage concepts, such as inheritance, to reason about things in our domain and not just rows and columns.

There is no question that Entity Framework is the future of data access for the Microsoft platform. The first release in August 2008, although somewhat deficient, was widely considered a good first step. Now, many releases later, Entity Framework 6 has matured into a full-featured data access technology ready for production use in both greenfield and legacy applications.

The concepts and patterns that you will learn as you work with the recipes in this book will serve you well into the future as Microsoft continues to evolve Entity Framework in the years to come.

Who This Book Is For

This book is for anyone who develops applications for the Microsoft platform. All of us who work in this field need access to data in our applications. We are all interested in more powerful and intuitive ways to reason about and program against the real-world objects in our applications. It makes much more sense for us to architect, design, and build applications in terms of customers, orders, and products rather than rows and columns scattered among tables locked away in a database. Because we can reason about problem space in terms of real-world objects, we have a lot more confidence in our design and in the code that we build. We are also better able to document and explain our applications to others. This makes our code much more maintainable.

Entity Framework is not just for development teams building custom applications. Microsoft is aggressively positioning the modeling concepts in Entity Framework to serve as the conceptual domain for Reporting Services and Integration Services as well as other technologies that process, report on, and transform data. Entity Framework is quickly becoming a core data access foundation for many other Microsoft technologies.

This book contains well over 100 recipes that you can put to work right away. Entity Framework is a large and complex topic. Perhaps it’s too big for a monolithic reference book. In this book, however, you will find direct and self-contained answers to just about any problem that you’re facing in building your Entity Framework-powered applications. Along the way, you’ll learn an enormous amount about Entity Framework.

What’s in This Book

We’ve organized the recipes in this book by topic. Often we’ve found that a recipe fits into more than one chapter, and sometimes we find that a recipe doesn’t fit perfectly into any chapter. We think it’s better to include all of the important recipes rather than just the ones that fit, so you might find yourself wondering why a particular recipe is in a certain chapter. Don’t worry. If you find the recipe useful, we hope that you can forgive its (mis)placement. At least we got it into the book.

The following is a list of the chapters and a brief synopsis of the recipes that you’ll find within them:

  • Chapter 1: Getting Started with Entity Framework. We explain the motivation behind Entity Framework. We also explain what the framework is and what it does for you.
  • Chapter 2: Entity Data Modeling Fundamentals. This chapter covers the basics in modeling. Here you’ll find out how to get started with modeling and with Entity Framework in general. If you’re just getting started, this chapter probably has the recipes you’re seeking.
  • Chapter 3: Querying an Entity Data Model. We’ll show you how to query your model using both LINQ to Entities and Entity SQL.
  • Chapter 4: Using Entity Framework in ASP.NET MVC. Web applications are an important part of the development landscape, and Entity Framework is ideally suited for ASP.NET. In this chapter, we focus on using the EntityDataSource to interact with your model for selects, inserts, updates, and deletes.
  • Chapter 5: Loading Entities and Navigation Properties. The recipes in this chapter cover just about every possibility for loading entities from the database.
  • Chapter 6: Beyond the Basics with Modeling and Inheritance. Modeling is a key part of Entity Framework. This is the second of three chapters with recipes specifically about modeling. In this chapter, we included recipes that cover many of the more complicated, yet all-too-common modeling problems that you’ll find in real-world applications.
  • Chapter 7: Working with Object Services. In this chapter, we included recipes that provide practical solutions for the deployment of your models. We also provide recipes for using the Pluralization Service, using the edmgen.exe utility, and working with so-called identifying relationships.
  • Chapter 8: Plain Old CLR Objects. Using code-generated entities is fine in many scenarios, but there comes a time when you need to use your own classes as EntityTypes. The recipes in this chapter cover Plain Old CLR Objects (POCO) in depth. They show you how to use your own classes and reduce code dependence on Entity Framework.
  • Chapter 9: Using Entity Framework in N-Tier Applications. The recipes in this chapter cover a wide range of topics using Entity Framework across the wire. We cover Entity Framework usage with WCF and ASP.NET Web API services, as well as related topics such as serialization and concurrency.
  • Chapter 10: Stored Procedures. If you are developing or maintaining a real-world, data-centric application, you most likely work with stored procedures. The recipes in this chapter show you how to consume the data exposed by those stored procedures.
  • Chapter 11: Functions. The recipes in this chapter show you how to create and use model-defined functions. We also show you how to use functions provided by Entity Framework, as well as functions exposed by the storage layer.
  • Chapter 12: Customizing Entity Framework Objects. The recipes in this chapter show you how to respond to key events, such as when objects are persisted. We also show you how to customize the way those events are handled.
  • Chapter 13: Improving Performance. For many applications, getting the best performance possible is an important goal. This chapter shows you several ways to improve the performance of your Entity Framework applications.
  • Chapter 14: Concurrency. Lots of instances of your application are changing the database. How do you control who wins? The recipes in this chapter show you how to manage concurrency.

About the Recipes

At present, there are three perspectives on model development in Entity Framework. Each of these perspectives is at a different level of maturity in the product and at a different level of use in the community.

The initial perspective supported by Entity Framework is called Database First. Using Database First, a developer starts with an existing database that is used to create an initial conceptual model. This initial model serves as the starting point for further development. As changes occur in the database, the model can be updated from these database changes. Database First was the initial perspective supported in Entity Framework. It is the best-supported approach, and it is widely used to migrate existing applications to Entity Framework.

Another perspective for model development is the Model-First approach. With Model First, the developer starts with a blank design surface and creates a conceptual model. Once the conceptual model is complete, Entity Framework can automatically generate a script to create a complete database for the conceptual model.

Finally, there is the Code-First perspective for model development. In this approach, there is no .edmx file, which encapsulates model and mapping information. Your objects create and use a model dynamically at runtime. This approach is gaining popularity quickly as it enables you to create and maintain your own domain classes, but still hook into Entity Framework features, like query generation, lazy loading, and change tracking.

In this book, we focus on both the Database-First and Code-First perspectives. Both are widely used across the community.

Many, if not most, developers in the Entity Framework community find themselves working with existing applications or developing models that are not readily supported by the other perspectives. We also have to share a dirty little secret: many existing applications don’t exactly use the best database designs. Way too often we find ourselves working with databases (of course, created by other, less-talented developers) that are poorly designed. As developers, sometimes working in larger organizations with lots of process control, or with lots of fragile legacy code, we can’t change the database enough to truly fix the design. In these cases, we simply have to work with the database design that we have.

Many of the recipes that we selected for this book take on the task of modeling some of these more challenged database designs. We’ve found hundreds of examples of these databases in the wild, and we’ve worked with many developers in the Entity Framework community who have struggled to model these databases. We’ve learned from these experiences, and we’ve selected a number of recipes that will help you solve these problems.

Stuff You Need to Get Started

Okay, what do you need? First off, you will need Microsoft’s latest software development environment. Microsoft Visual Studio 2013 comes complete with full support for Entity Framework 6. If you are using Microsoft Visual Studio 2012, you can easily install Entity Framework 6. Keep in mind that the Visual Studio 2013 Express Edition is freely available. The other versions of Visual Studio fully support Entity Framework.

You’ll need a database. Microsoft SQL Server 2012 is the preferred choice, but there are Entity Framework providers for databases from other vendors. Keep in mind that Microsoft SQL Server 2012 Express is freely available. Make sure that you apply the latest service packs and updates. These recipes were built and tested using Microsoft SQL Server 2012. Previous versions of SQL Server or other databases may not play well with a few of the recipes.

Code Examples

This book is all about recipes that solve very specific problems in a way that allows you to apply the solution directly to your code. Feel free to use and adapt any of the code you find here to help build or maintain your applications. Of course, it’s not okay to copy large parts of this material and distribute it for fun or profit. If you need to copy large parts of this material, contact our publisher, Apress, to get permission.

If you use our code publicly (in blogs, forums, and so on), we would appreciate, but don’t require, some modest attribution, such as author, title, and ISBN.

We’ve taken a decidedly low-tech approach in the code in each recipe. We’ve tried not to clutter the code with unnecessary constructs and clever tricks. In the text, we show just the code of interest, but we also show enough to give the proper context. In the download for the code, we have complete solutions for each recipe. The solutions build simple applications that you can modify and run over and over again to play with various changes that suit your needs.

The Database

Of course, there is more to each recipe than just the code. We created a single database for all of the recipes. This makes it much easier to work through the recipes because there is just one database to create in your development environment.

To keep some sanity in the table names and provide at least a little organization, we created a schema for each chapter. The recipes in the chapter use the tables in the corresponding schema. In the text, we often show database diagrams similar to the one in Figure 0-1. This helps clarify the table structure with which we’re working. Each table in a diagram is annotated (courtesy of SQL Server Management Studio) with the name of the table and the schema for the table. Because we reuse table names throughout the book (we’re just not creative enough not to), this helps to keep straight exactly which tables we’re referring to in the database.

9781430257882_FM-01.jpg

Figure 0-1. Each database diagram in the text has the schema name next to the table name

We’ve also provided the complete set of database diagrams for each recipe as part of the database. If something isn’t clear from just the tables, especially when several tables are involved, it often helps to look at the diagram to sort things out.

Apress Website

Visit the Apress website for this book (http://apress.com/9781430257882) for the complete code download as well as the database with all of the tables and database diagrams used in the book. Please look for the “Source Code” link underneath the cover image.

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

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