Book Image

Code-First Development with Entity Framework

By : Sergey Barskiy
Book Image

Code-First Development with Entity Framework

By: Sergey Barskiy

Overview of this book

<p>Entity Framework Code-First enables developers to read and write data in a relational database system using C# or VB.NET. It is Microsoft's answer to demand for an ORM from .NET developers.</p> <p>This book will help you acquire the necessary skills to program your applications using Entity Framework. You will start with database configuration and learn how to write classes that define the database structure. You will see how LINQ can be used with Entity Framework to give you access to stored data. You will then learn how to use Entity Framework to persist information in a Relational Database Management System. You will also see how you can benefit from writing ORM-based .NET code. Finally, you will learn how Entity Framework can help you to solve database deployment problems using migrations.</p>
Table of Contents (15 chapters)
Code-First Development with Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

I have been writing applications on the Microsoft platform for almost 2 decades. Many, if not all of them, use databases to persist user data. I have used many technologies to access data, starting with ADO.NET. Object Relational Mapping (ORM) tools, have many advantages over ADO.NET. They allow developers to write data access code faster and safer. ORM tools have been designed to solve impedance mismatch problems between object-oriented programming and relational databases. Microsoft's Entity Framework is the company's answer to the demand for an ORM from .NET developers. This book is the guide that will help you acquire the necessary skills to program your applications using Entity Framework.

This book centers on the Code-First approach with Entity Framework, which has become the most common way of using the technology. Code-First allows developers to control the entire data access layer of their applications from the .NET code. This approach simplifies and streamlines the entire application development life cycle, keeping developers coding inside Visual Studio, the only tool they need to use Entity Framework.

The books starts with the basic concepts of defining the database structure via C# and VB.NET code, then progresses to full data access. Chapters cover create, read, update, and delete operations (CRUD) with Entity Framework. It also shows how to update the Relational Database Management System, (RDBMS) structure, via the migrations API. It explores aspects of data access in both .NET languages using the Languages INtegration Query (LINQ), API. Because of Microsoft's continuous commitment to both C# and VB.NET, the book contains examples in both languages in every chapter.

I have been using Entity Framework since 2008, and I felt that I had the necessary experience to write a book on the subject. I spoke on the topic on many conferences and events and saw tremendous interest in creating a concise guide to Entity Framework. This was one of my primary motivations in creating a shorter textbook. I read many technical books while working in the industry, and I myself, at times, had trouble maintaining the focus while reading 800-page technical books. They definitely have a place in the industry and are very useful. However, I feel they are intimidating for the developers who are just getting started with a particular technology. My hope is that this book will get you going quickly on the new topic and have you writing data access code in a few hours. You should be able to master the foundation behind Entity Framework with this book quickly and easily.

What this book covers

Chapter 1, Introducing Entity Framework, gives us an understanding of what the, Object Relational Mapping (ORM) technology brings to developers. You learn the history of Entity Framework as an example of an ORM. We study the architecture behind the Entity Framework technology.

Chapter 2, Your First Entity Framework Application, teaches us how to create our first project that uses Entity Framework. We create classes that map to database tables. We observe how our target database is created when the project is run. Finally, we save and retrieve our first data from the created database.

Chapter 3, Defining the Database Structure, dives deep into details of mappings between classes and tables. We create maps between properties to columns as well as rules that govern such mappings. We define relationships between classes that translate into relationships between tables. We exercise multiple approaches that can be used to define the mappings.

Chapter 4, Querying, Inserting, Updating, and Deleting Data, discusses how to use the LINQ API, that allows developers to retrieve the data from the database. We sort, filter, and perform element operations and use quantifiers. We query related entities. You learn the advantages and pitfalls of eager and lazy loading. We insert, delete, and update the data.

Chapter 5, Advanced Modeling and Querying Techniques, dives deeper into modeling and querying techniques. We use complex types to have more consistency in the database structures. We create an explicit table and column names. We define structures that use table and entity splitting. We use projections in queries to make them more efficient and summarize our data. We page the data for retrieval, breaking it up for presentation to the users. We use joins to create queries that use related entities.

Chapter 6, Working with Views, Stored Procedures, the Asynchronous API, and Concurrency, shows how to access with database views from Entity Framework. We query data via stored procedures using the Entity Framework API. We perform create, update, and delete operations with stored procedures. We exercise Entity Frameworks and the asynchronous API and learn the advantages and pitfalls of asynchronicity. We implement concurrency handling, learning to handle the situation when multiple users attempt to update the dame data.

Chapter 7, Database Migrations and Additional Features, shows how to enable migrations on our Entity Framework project, creating and updating the database schema without data loss. We use implicit migrations first, then create explicit migrations, customizing our migration code. We use common aspects of the migrations API, adding columns and specifying default values. We apply migrations using multiple approaches. We create migrations from an existing database. We dive briefly into useful Entity Framework features, not covered previously.

Appendix, Answers to Self-test Questions, contains answers to questions you will find throughout the book.

What you need for this book

In order to run the sample code, you will need access to Visual Studio 2013. You can use free Community Edition. You also need an instance of SQL Server 2008 R2 or higher on your machine. The free Express edition of SQL Server can be used.

Who this book is for

This book is intended for software developers with some prior experience in the Microsoft .NET framework who want to learn how to use Entity Framework. Maybe you have used SQL for years, but want to write data access code more easily and safely, using C# or VB.NET instead. This book is for you if you want to learn how to use this Microsoft ORM to create strongly typed data access logic, or want to get your database changes deployed with minimal effort. This book will get you up and going quickly, providing many examples for C# and VB.NET programmers that illustrate all the key concepts of Entity Framework.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, user input are shown as follows: "You also need at least one class that represents the database itself, which will inherit from DbContext."

A block of code is set as follows:

Public Class Person
 Property PersonId() As Integer
 Property FirstName() As String
 Property LastName() As String
End Class

Any command-line input or output is written as follows. "If we need to get detailed help for the PowerShell commandlet Enable-Migrations, we just need to type Get-Help Enable-Migrations."

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at http://www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.