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

Entity Framework Model First approach


The Model First approach is useful when we don't have a database to start with. Using this approach, we can create our models directly in the Visual Entity Designer, and then create the database schema based on our model. In this approach too, we can create entities and their properties, define relationships and constraints between the entities, and create inheritance relationships.

In this approach, we will first create the conceptual model, and based on this the database and the strongly typed objects will be created. To start with the Model First approach, we first need to add an ADO.NET EDM.

Let's try to add an ADO.NET EDM in our application:

Add a new ADO.NET EDM

Since we are planning to start with an empty model, we have to select an empty model in the wizard. The following screenshot shows the wizard step that asks you to select the option. We have to select Empty model in this step:

ADO.NET EDM wizard step 1—select the Model First approach

Once we choose the empty model, Entity Framework will show us the Visual Entity Designer. Now we can add the entities and relations to the designer area from the toolbar. The Entity Designer and the toolbar will look something like this:

An empty Visual Entity Designer after selecting the Model First approach

Let's try to add a new entity to our ToDo application and add properties to that model. Let's add a few properties to our ToDo model:

Adding a new property from Visual Entity Designer

Let's add the scalar properties needed for our ToDo model. Let's make the ID field a primary key field, a string property for ToDoItem, and a Boolean property for IsDone:

Visual Studio's Property panes showing the properties of a conceptual model

The created entity will look something like this:

The final model created from the Visual Entity Designer

Once we have the conceptual model created for our application, we have to create the database from this conceptual model. This can be done by right-clicking on the entity and choosing Generate Database from Model:

Generating database scripts from the Visual Entity Designer

This will prompt you to select the database connection. This wizard will not create the actual database, but it will generate the DDL for us, and we will have to use that DDL to create the database ourselves:

Wizard step to select the connection string to be used for a generated database script

After we select the database connection, the wizard will give us the SQL to create the database on the specified connection:

Wizard step showing the create database script

We can now copy this SQL and create the database. The Entity Framework has already created connectionString to use this connection, and wired up the DBContext class to perform the operations on this database. Let's take a look at the generated DbContext class:

public partial class ToDoModelContainer : DbContext
{
  public ToDoModelContainer()
    : base("name=ToDoModelContainer")
  {
  }

  public virtual DbSet<ToDo> ToDoes { get; set; }
}

The strongly typed objects will also be generated from the conceptual model. Let's take a look at our generated ToDo class:

public partial class ToDo
{
  public ToDo()
  {
    this.IsDone = false;
  }

  public int Id { get; set; }
  public string ToDoItem { get; set; }
  public bool IsDone { get; set; }
}

If we are using the Model First approach, one important thing to consider is that the incremental changes in our model will not perform the incremental updates to the database. Rather, it will give us the SQL to create the updated database from the ground up.

Tip

If we want to perform incremental updates to the database, we can use the Database Generation Power Pack from Microsoft (https://visualstudiogallery.msdn.microsoft.com/df3541c3-d833-4b65-b942-989e7ec74c87/). Using this, we can perform incremental updates to the database.