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

Chapter 2: Your First Entity Framework Application


Q1. DbSet<T> is the class you should be using to define a property in your context class that corresponds to a table in your database. The type parameter T represents a class that defines that table's structure in terms of .NET.

Q2. As DbContext holds an underlying connection to the database, you should utilize the IDisposable pattern and call Dispose on your context when you are done using it. You can also use the Using keyword to achieve the same.

Q3. The Find method can be used to locate a row in the database. It takes one or more parameters corresponding to the values of the primary key. If you have a single column that defines the primary key, only one value is needed. Multiple parameter values are reserved for tables with complex multicolumn primary keys.

Q4. You can use the Remove method and pass in an instance you would like to be deleted from the database when SaveChanges is called on your context.

Q5. You can just find the corresponding object and set its LastName property to new values. Then, you can call SaveChanges to commit the updated data to the database. You can use the Find method or LINQ to locate the matching row in the database. We will see other methods to issue updates in later chapters.

Q6. You will get an exception because no initializer is used. We will see in later chapters how migrations solve this problem.