Book Image

Mastering Entity Framework

By : Rahul Rajat Singh
Book Image

Mastering Entity Framework

By: Rahul Rajat Singh

Overview of this book

<p>Data access is an integral part of any software application. Entity Framework provides a model-based system that makes data access effortless for developers by freeing you from writing similar data access code for all of your domain models.</p> <p>Mastering Entity Framework provides you with a range of options when developing a data-oriented application. You’ll get started by managing the database relationships as Entity relationships and perform domain modeling using Entity Framework. You will then explore how you can reuse data access layer code such as stored procedures and table-valued functions, and perform various typical activities such as validations and error handling. You’ll learn how to retrieve data by querying the Entity Data Model and understand how to use LINQ to Entities and Entity SQL to query the Entity Data Model.</p>
Table of Contents (19 chapters)
Mastering Entity Framework
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Managing database initialization


When we run our Entity Framework Code First application for the first time, Entity Framework will do the following:

  1. Check the DbContext class that is being used.

  2. Find the connectionString that should be used with this context class.

  3. Find the domain entities and extract the schema-related information.

  4. The database will be created.

  5. Data will be inserted into the system.

What will happen if the database already exists? What will happen if the application is run a second time? Should it create the database again or simply use the existing database? These are a few questions that can be answered simply by understanding the database initialization process of Entity Framework.

Once the schema information is extracted, Entity Framework will use database initializers to push the schema information to the database. There are various possible strategies for database initializers, and Entity Framework's default strategy is to create the database if it doesn't exist, and use...