Preface

Microsoft’s principal data access technology, ADO.NET Entity Framework, has had two major releases as part of the .NET Framework. NET 3.5 brought us the first version of Entity Framework, which is covered in the first edition of Programming Entity Framework. In 2010, Microsoft .NET 4 was released; it contained the next version of Entity Framework, referred to as Entity Framework 4. The completely revised second edition of Programming Entity Framework was dedicated to teaching readers how to use this version of Entity Framework in Visual Studio 2010.

When .NET 4 was released, the Entity Framework team was already hard at work on a new addition, called Code First, to provide an alternative to building the Entity Data Model that is core to Entity Framework. Rather than using a visual designer, Code First allows you to create the model from your existing classes.

This book is dedicated to teaching readers how to use Code First to build and configure a model based on the classes in your business domain. While Code First can do much of the job of inferring a model from your classes, there is quite a lot that you can do to affect the model that Code First creates.

In this book, you will learn what Code First does by default (aka convention) and how to perform further configuration to affect how it understands your properties, classes, relationships, and the database schema they map to—whether you use Code First to help create a database or you want to use it with an existing database. With this knowledge, you can reap the benefits of the Entity Framework while leveraging existing classes or those classes you might be building for a new software project.

Audience

This book is designed for .NET developers who have experience with Visual Studio and database management basics. Prior experience with Entity Framework is beneficial but not required. The code samples in this book are written in C#, with some of these samples also expressed in Visual Basic. There are a number of online tools that you can use to convert snippets of C# into Visual Basic.

Contents of This Book

This book contains eight chapters.

Chapter 1

  • This chapter provides a high level, end-to-end overview of Code First. You’ll find sample code, but there are no walkthroughs in this first chapter. The chapter winds up with a discussion of what you won’t find in Code First, so that you can approach the technology with the correct expectations.

Chapter 2

  • In this chapter, you will get to jump right in to the code. The chapter gives you a chance to work hands-on with Code First (or just read along if you prefer) as you work with a simple class to see some of the basic default behavior and perform some simple configurations using the two mechanisms for configuring: Data Annotations and the Fluent API. You’ll see how Code First is able to automatically create a database for you using default behavior. Through the next four chapters you’ll lean on this database creation default, and then in Chapter 6, you’ll learn how to work with existing databases and exert more control over the database.

Chapter 3

  • This is the first of three chapters that dive deeply into Code First convention and configuration. You’ll learn about the presumptions the Code First convention makes about the attributes of properties (e.g., the length of strings) and how that gets interpreted into the conceptual model and the database. You’ll also learn how to perform configuration using Data Annotations and Fluent API to control the outcome of the model and database.

Chapter 4

  • In this chapter, you’ll focus on relationships between your classes and how those work out in the model that Code First infers and in the database. Code First convention is able to infer the most common scenarios when classes have relationships between them. We’ll look closely at the nuances in your classes that will drive Code First’s assumptions and then how to ensure that Code First knows exactly what you want it to do, again by configuring with Data Annotations or the Fluent API.

Chapter 5

  • This chapter focuses on how classes map to the database. This information will be especially important when you are mapping your classes to an existing database. Simple mappings, such as specifying table names or column names and types, can make a huge difference. You’ll learn about the default mappings to the database when you have inheritance hierarchies defined between your classes and how to drive Table Per Hierarchy, Table Per Type and even Table Per Concrete Type mappings to the database. You’ll also learn how to map a single entity to multiple tables or, conversely, multiple entities to a single table.

Chapter 6

  • This chapter is where you finally get to stray from the default database creation behavior. You’ll learn how to control Code First’s determination of the database name and location, whether you do this through connection strings or some lower-level code in the Code First API. You’ll also find some additional tricks for controlling connections and more.

Chapter 7

  • In this chapter, you’ll dig farther into Code First to see how to perform some advanced techniques. You’ll see how to prevent Code First from worrying about keeping the database in sync with your model when you want to take over control of that task. You’ll also learn about the default model caching and how to override it to solve problems like targeting multiple database providers in the same application instance. Other advanced topics are addressed as well.

Chapter 8

  • This book was written about the features of Code First based on the Entity Framework 4.2 release. At the time of this writing, a number of Community Technical Previews demonstrate some of the features that Code First will gain in upcoming releases. This chapter shares available information about these future releases.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This icon signifies a tip, suggestion, or general note.

Caution

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Programming Entity Framework: Code First by Julia Lerman and Rowan Miller (O’Reilly). Copyright 2012 Julia Lerman, Rowan Miller, 978-1-449-31294-7.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://shop.oreilly.com/product/0636920022220.do

To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Special thanks to technical reviewers Andrew Peters, from the Entity Framework team, and Suzanne Shushereba, a software developer at Fletcher Allen Health Care in Burlington, Vermont (and a friend). Andrew leveraged his expertise in Code First to ensure that we hadn’t made any technical gaffes. Suzanne was new to Code First but not Entity Framework. She not only read the text to point out where we could provide a better explanation for a newbie, but she also followed along with the walkthroughs in Visual Studio to help us find places where providing additional code would be more helpful.

Thanks to Microsoft for making it easy for Rowan to participate in this project.

Thanks once again to O’Reilly Media for providing their support.

Get Programming Entity Framework: Code First now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.