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

Creating a new database based on .NET classes


When it comes to working with data, we will need to create at least two types of classes. We need to create one or more classes to map the tables in the database, where each class represents a row of data in the corresponding table. You also need at least one class that represents the database itself, which will inherit from DbContext. To start with, let's create a class with the same structure as the Person table from the Chapter 1, Introducing Entity Framework, with properties for id and the first and last names. Here is how the class looks in C#:

public class Person
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Tip

Downloading the example code

You can download the example code fies 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...