Code-First Development With Entity Framework - Sample Chapter
Code-First Development With Entity Framework - Sample Chapter
"Community
Experience
Distilled"
C o m m u n i t y
Sergey Barskiy
Code-First Development
with Entity Framework
D i s t i l l e d
Code-First Development
with Entity Framework
Take your data access skills to the next level with Entity Framework
E x p e r i e n c e
Sergey Barskiy
Code-First Development
with Entity Framework
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.
Chapter 1
What is ORM?
When it comes to business software, almost all of it needs to store data that pertains to
its functions. For many decades, Relational Database Management System (RDBMS)
has been a go-to data storage for developers. ORM is a set of technologies that allows
developers to access RDBMS data from an object-oriented programming language.
There are other RDBMSes available, such as SQL Server, Oracle, DB2, MySQL, and
many more. These database systems share some common characteristics. Each system
supports one or more databases. Databases consist of many tables. Each table stores
data in a tabular format, divided into columns and rows. Data rows in multiple tables
may relate to each other. For example, a person's details stored in the Person table can
have phone numbers stored in a separate Phones table.
[1]
In the following screenshot, you can see a table that allows you to store a person's
information, specifically their first and last names, along with a unique identifier for
each person. This type of storage, where similar data items are grouped together into
tabular structures is typical:
Each column can also be constrained in some ways. For example, PersonId is an
integer column. LastName is nvarchar(50) column, which means you can store
Unicode data of variable size in it, up to 50 characters. You will see in subsequent
chapters how we describe this information using Entity Framework.
The data stored in each column and row combination is scalar data, such as number
or string. When software needs to persist or retrieve data, it must describe its intent,
such as insert or select query, using the database-specific language called Structured
Query Language (SQL). SQL is a common standard for all relational database systems,
as issued by the American National Standards Institute (ANSI). However, some
database systems have their own dialect on top of the common standard. In this book,
we are not going to dive into the depths of SQL, but some concepts are important
to understand. There are some basic commands that we need to look at. These are
typically described as CRUD. CRUD stands for Create, Retrieve, Update, and Delete.
For example, if you want to retrieve or query the data from the preceding example,
you would type the following:
SELECT PersonId, FirstName, LastName
FROM Person
Chapter 1
For example, if we change the column name in our database from FirstName to
First_Name, our code would still compile just fine, but would throw an exception
when we try to run it. Moreover, the data in the database was stored as scalar values
organized in columns and rows in a table, but our destination was an object or object
graph. As you can see, this way of accessing the data has a number of issues.
First of all, there is a type mismatch between RDBMS column types and .NET
types. Second, there is a mismatch between storage, which is a collection of scalar
values, and destination, which is an object with properties. To further complicate
the situation, our person object could also have a complex property that contains
a list of phone numbers, which would be represented by a completely different
table. These problems are collectively referred to as impedance mismatch between
object-oriented programming and relational databases.
The set of tools called ORM came about to solve this mismatch problem. An ORM
tool represents data stored in database tables as objects, native to a programming
language, such as .NET languages, C#, and VB.NET. ORM tools have many
advantages over the traditional code, such as ADO.NET code that we mentioned.
They expose the data using native .NET types. They expose related data using simple
.NET properties. They provide compile time checking. They solve the problem with
typos. Developers do not have to use SQL, a different language. Instead in the .NET
world, developers use Language INtegrated Query (LINQ) to query the data. LINQ
is simply part of C# and VB.NET languages. We will cover the basics of LINQ in
subsequent chapters. By the same token, programmers use an ORM tool's API to
persist data to the database. Finally, as we will see later, you will write less code.
Less code means fewer bugs, right?
[3]
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.
[4]
Chapter 1
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!
[5]
Self-test questions
Q1. Which of these problems does an ORM tool solve?
1. Types in RDBMS and .NET framework are the same
2. Impedance mismatch between RDBMS and object-orientated programming
3. Learning SQL is hard
Q2. Developers must write SQL queries to work with Entity Framework. True or false?
Q3. What is the name of the technology that Entity framework uses to apply
structural changes to the target database?
1. Updates
2. Conversions
3. Migrations
Q4. Which is the key class that represents database abstraction with the Entity
Framework Code-First approach?
1. DbContext
2. ObjectContext
3. DataContext
Q5. Entity Framework can only work with Microsoft databases, such as SQL Server.
True or false?
Summary
In this chapter, we took a look at how data is stored in RDBMS systems. We saw the
shortcomings of using embedded SQL to access the data. We understood what ORM
tools are all about and what problems they solve. We examined the history behind
Entity Framework. We saw the capabilities of Entity Framework. Finally, we had a
brief excursion into the Entity Framework architecture.
In the next chapter, we will actually build our first application based on Entity
Framework Code-First.
[6]
www.PacktPub.com
Stay Connected: