Entity Framework Code First and Code First Migrations for Oracle Database
Overview
Purpose
This tutorial demonstrates how to use Entity Framework (EF) Code First with Oracle Data Provider for .NET (ODP.NET).
You will create .NET classes in Entity Framework, which will then create Oracle database tables and add data to those tables.
Lastly, you will modify those classes using Code First Migrations and propagate the changes so that they are reflected in the database tables.
Time to Complete
Approximately 20 minutes.
Introduction
Microsoft Entity Framework abstracts the relational, logical database schema and presents a conceptual schema to the .NET application.
It provides object-relational mapping for .NET developers. Code First allows developers to create .NET classes,
which in turn can be persisted as database objects. Code First Migrations allows developers to evolve the code-based
model so that changes are accurately reflected in the persistent database store.
Starting with Oracle Data Access Components (ODAC) 12c Release 3 (12.1.0.2.1), ODP.NET supports Code First and
Code First Migrations from Entity Framework 6. ODP.NET provides the data access to store the EF model inside
the Oracle Database and evolve it.
This tutorial will guide you in creating an EF application that creates "Employee" and "Department" classes, then adds data
to these class objects. When the application is run, these classes will be persisted as database tables with the data entered as rows.
You will then modify the class by adding a new property. That property will then be added to the "Departments" table as a new column
via Code First Migrations.
Prerequisites
Before starting this tutorial, you should:
Install Microsoft Visual Studio 2013 or later with .NET Framework 4.5 or later.
Code First doesn't require Oracle Developer Tools for Visual Studio which provides integration with Server Explorer and Visual Studio designers. This tutorial uses Server Explorer to browse the Oracle schema to view Code First database changes. To be able to do this, download and install Oracle Developer Tools for Visual Studio (12.1.0.2) or later from OTN.
This OBE will connect to the Oracle database with the HR schema, though any schema can be used as long as there are no naming conflicts.
Code First
In this section, you will create .NET "Employee" and "Department" classes and save data using these classes.
When the application is run, EF Code First will create database tables to represent these classes
and store a row each time the application saves data to a class instance.
Open the NuGet project that you created in the Using NuGet to Install and Configure Oracle Data Provider for .NET OBE and perform the following steps:
Open the App.config. Modify the user id and password under the <connectionStrings> section to connect to the HR schema. Typically, the HR schema credentials are: User Id=hr and Password=hr.
Note the name of the connection string, OracleDbContext. We will be using that name shortly in the Code First application.
Copy the code snippet from the Programcs.txt file and paste it into the Program.cs of the Visual Studio project. The application needs Entity Framework 6 and ODP.NET assemblies and configuration from the NuGet steps.
Let's review the code. The application first connects to an Oracle database using the OracleDbContext connection string that was configured in the App.config.
It then creates an instance of the Employee and Department classes and adds data to them as seen in the screenshot below.
Next, we see how these two classes are defined by the .NET application. They each contain several properties
that can be retrieved or saved. One property is commented; it will be added in the last part of this tutorial.
If you do not wish to use the HR schema, then change the last line of the program. Replace the HR in the modelBuilder.HasDefaultSchema("HR"); statement with the name of the schema you wish to use.
Click (Start) to run the application.
This will create the "Employees" and "Departments" tables in the database and add one row to each of them.
Note that these table names are case-sensitive.
Let's check whether these tables and data were added. After completing the execution, expand the HR.ORCL connection in Server Explorer.
Note this step requires that you have installed and configured Oracle Developer Tools for Visual Studio.
Classes do not always stay static. They can change based on new business requirements and the database schema must change with
the class definition. We will now make a change to the "Employee" class to simulate this type of situation.
With a few simple commands, the change will be propagated to the database to keep in sync with the application.
This is called Code First Migrations.
When you executed the previous Code First application, you may have noticed that a third table, _MigrationHistory, was created. This table tracks changes to the Code First classes.
For this part of the OBE, you will add a new Location property to the "Employee" class and have this change reflected in your database. Perform the following steps:
Select View > Other Windows > Package Manager Console.
The Package Manager console opens. This console is where you enter in Code First Migrations commands
to propagate .NET class changes to the the database schema.