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

The capabilities of Entity Framework


Entity Framework can do a lot for us as Microsoft developers. First of all, it is capable of exposing the database as a set of objects. It does so by utilizing a couple of key classes. First and foremost, you need to be aware of DbContext. This class is at the heart of Entity Framework Code-First. At a high level, it is a database abstraction. Databases consist of tables, each consisting of rows and columns. DbContext in turn has generic collection properties; each of which can be typed as DbSet<TRowType>, corresponding to each table. Each object within the collection, referred to as an entity, represents a row in the corresponding table. Columns are defined by properties of the TRowType class that is specified as a generic argument of each collection.

Once this structure is laid out, you are capable of querying the underlying database by using LINQ queries. If you add a brand new instance of the TRowType class to its parent collection and then save the changes using the DbContext API, this new object will become a row in the corresponding table, where each property value of that object will become a column value in the target row. On top of this, Entity Framework has capabilities to represent other database artifacts, such as procedures and functions. You will be able to query the data using functions, just like tables using LINQ again. The question of evolving the database structure is an important one. In most cases, you will need to add columns and tables, as your application changes. Entity Framework addresses this need via the Migrations feature. This ability will allow you to alter the database structure through C# code. In addition to adding and deleting tables and columns, you will be able to add indexes. Migrations allow developers to evolve a schema without data loss. As you can see, Entity Framework exposes everything you need to access the data in your C# or VB.NET code without wiring SQL and treats your database as another part of your overall application code. You can check migrations code into source control, since it is also C# code!