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

A brief history of Entity Framework


Over the years, there have been many ORM tools entering the market; some commercial, others open source. Microsoft developed its own tools. First one was LINQ to SQL, which was built on .NET 3.5. This ORM only worked with SQL Server and SQL Server Compact. Entity Framework, which first shipped in 2008, was the second attempt. It had a number of advantages over LINQ to SQL. First of all, it had provider architecture, thus was open to working with all relational database engines, not just SQL Server, given that a provider was written for the engine in question. All major RDBMSes have Entity Framework providers at this point in time.

Entity Framework went through a few revisions. In the first version, only Database First approach was supported. What this meant was that you would point the designer to an existing database. As a result, code was generated that would contain a database and table abstractions. In addition to the code, an EDMX file was also created. This XML file contained Entity Data Model. It consisted of three models: logical, storage, and mapping. The logical, sometimes called conceptual, model is the one you will code against in C# or VB.NET. Storage model describes how data is stored in a database. The mapping model, as the name implies, provides the mapping between logical and storage models. If you were to change anything in the database, you would need to refresh the generated model. The C# or VB.NET code is also generated again. The mapping model has a class based on ObjectContext that has collection properties for each table in the database. Each collection is a generic collection, where collection item type is inherited from a base class in Entity Framework. Each class has properties that correspond to columns in the matching table.

In the second revision, version 4, the Model-First approach was supported as well. With this approach, you can use design surface to create entities, and then the designer would produce the SQL script to generate the database. With this approach, the EDMX file was still created, and the final result was the same as with the Database First approach. Developers had access to the same set of classes to give them the ability to persist and query data.

Finally, the Entity Framework Code-First approach was shipped in version 4.1. This approach eliminated the need for the EDMX file. It also eliminated the dependency on Entity Framework base classes that each entity in the model inherited from. As a result, the code became more testable. This approach also eliminated the need for the designer. You could just type your classes, and they would automatically be mapped to tables in the database. There have been subsequent Entity Framework Code-First releases after the the initial 4.1 version.