Entity Framework Code First and Code First Migrations For Oracle Database
Entity Framework Code First and Code First Migrations For Oracle Database
2020 Ent ty Framework Code F rst and Code F rst M grat ons for Oracle Database
Ent ty Framework Code F rst and Code F rst M grat ons for Oracle
Database
Overview
Purpose
Th s tutor al demonstrates how to use Ent ty Framework (EF) Code F rst w th Oracle Data Prov der for .NET (ODP.NET).
You w ll create .NET classes n Ent ty Framework, wh ch w ll then create Oracle database tables and add data to those
tables. Lastly, you w ll mod fy those classes us ng Code F rst M grat ons and propagate the changes so that they are
reflected n the database tables.
T me to Complete
Introduct on
M crosoft Ent ty Framework abstracts the relat onal, log cal database schema and presents a conceptual schema to the
.NET appl cat on. It prov des object-relat onal mapp ng for .NET developers. Code F rst allows developers to create
.NET classes, wh ch n turn can be pers sted as database objects. Code F rst M grat ons allows developers to evolve the
code-based model so that changes are accurately reflected n the pers stent database store.
Start ng w th Oracle Data Access Components (ODAC) 12c Release 3 (12.1.0.2.1), ODP.NET supports Code F rst and
Code F rst M grat ons from Ent ty Framework 6. ODP.NET prov des the data access to store the EF model ns de the
Oracle Database and evolve t.
Th s tutor al w ll gu de you n creat ng an EF appl cat on that creates "Employee" and "Department" classes, then adds
data to these class objects. When the appl cat on s run, these classes w ll be pers sted as database tables w th the data
entered as rows. You w ll then mod fy the class by add ng a new property. That property w ll then be added to the
"Departments" table as a new column v a Code F rst M grat ons.
Prerequ s tes
1. Install M crosoft V sual Stud o 2013 or later w th .NET Framework 4.5 or later.
4. To beg n the tutor al, complete the Us ng NuGet to Install and Conf gure Oracle Data Prov der for .NET OBE.
The OBE w ll gu de you n nstall ng and conf gur ng Ent ty Framework 6 and ODP.NET plus creat ng a new V sual
Stud o console project.
5. Code F rst doesn't requ re Oracle Developer Tools for V sual Stud o wh ch prov des ntegrat on w th Server
Explorer and V sual Stud o des gners. Th s tutor al uses Server Explorer to browse the Oracle schema to v ew
Code F rst database changes. To be able to do th s, download and nstall Oracle Developer Tools for V sual
Stud o (12.1.0.2) or later from OTN.
Th s OBE w ll connect to the Oracle database w th the HR schema, though any schema can be used as long as there
are no nam ng confl cts.
Code First
In th s sect on, you w ll create .NET "Employee" and "Department" classes and save data us ng these classes. When the
appl cat on s run, EF Code F rst w ll create database tables to represent these classes and store a row each t me the
l t d t t l t
https://www.oracle.com/webfolder/technetwork/tutor als/obe/db/dotnet/CodeF rst/ ndex.html#overv ew 1/9
05.12.2020 Ent ty Framework Code F rst and Code F rst M grat ons for Oracle Database
appl cat on saves data to a class nstance.
1. Open the NuGet project that you created n the Us ng NuGet to Install and Conf gure Oracle Data Prov der
for .NET OBE and perform the follow ng steps:
Open the App.conf g. Mod fy the user d and password under the <connect onStr ngs> sect on to connect to
the HR schema. Typ cally, the HR schema credent als are: User Id=hr and Password=hr. Note the name of the
connect on str ng, OracleDbContext. We w ll be us ng that name shortly n the Code F rst appl cat on.
2. Copy the code sn ppet from the Programcs.txt f le and paste t nto the Program.cs of the V sual Stud o project.
The appl cat on needs Ent ty Framework 6 and ODP.NET assembl es and conf gurat on from the NuGet steps.
Let's rev ew the code. The appl cat on f rst connects to an Oracle database us ng the OracleDbContext
connect on str ng that was conf gured n the App.conf g.
It then creates an nstance of the Employee and Department classes and adds data to them as seen n the
screenshot below.
Next, we see how these two classes are def ned by the .NET appl cat on. They each conta n several propert es
that can be retr eved or saved. One property s commented; t w ll be added n the last part of th s tutor al.
If you do not w sh to use the HR schema, then change the last l ne of the program. Replace the HR n the
modelBu lder.HasDefaultSchema("HR"); statement w th the name of the schema you w sh to use.
3. Cl ck (Start) to run the appl cat on. Th s w ll create the "Employees" and "Departments" tables n the database
and add one row to each of them. Note that these table names are case-sens t ve.
4. Let's check whether these tables and data were added. After complet ng the execut on, expand the HR.ORCL
connect on n Server Explorer. Note th s step requ res that you have nstalled and conf gured Oracle Developer
Tools for V sual Stud o.
https://www.oracle.com/webfolder/technetwork/tutor als/obe/db/dotnet/CodeF rst/ ndex.html#overv ew 4/9
05.12.2020 Ent ty Framework Code F rst and Code F rst M grat ons for Oracle Database
Expand Tables under the HR.ORCL connect on. Not ce that the HR.Employees and HR.Departments are
created as def ned by the class def n t ons.
Ver fy that t reflects the same data that was nserted by the appl cat on.
Classes do not always stay stat c. They can change based on new bus ness requ rements and the database schema
must change w th the class def n t on. We w ll now make a change to the "Employee" class to s mulate th s type of
s tuat on. W th a few s mple commands, the change w ll be propagated to the database to keep n sync w th the
https://www.oracle.com/webfolder/technetwork/tutor als/obe/db/dotnet/CodeF rst/ ndex.html#overv ew 6/9
05.12.2020 Ent ty Framework Code F rst and Code F rst M grat ons for Oracle Database
appl cat on. Th s s called Code F rst M grat ons.
When you executed the prev ous Code F rst appl cat on, you may have not ced that a th rd table, _M grat onH story,
was created. Th s table tracks changes to the Code F rst classes.
For th s part of the OBE, you w ll add a new Locat on property to the "Employee" class and have th s change reflected
n your database. Perform the follow ng steps:
1. Select V ew > Other W ndows > Package Manager Console. The Package Manager console opens. Th s
console s where you enter n Code F rst M grat ons commands to propagate .NET class changes to the the
database schema.
2. In the console, type Enable-Migrations. Th s step enables Code F rst M grat ons.
3. In the Program.cs, uncomment the Locat on attr bute n the Employee class to s mulate add ng an attr bute to
the .NET class.
4. In the Package Manager console, type Add-Migration First. Th s scaffolds a m grat on scr pt for the model
change.
5. Type Update-Database n the console. Th s appl es the changes to the database schema.
6. We should now be able to see the change n the database. In the Server Explorer, double-cl ck Employees.
You can reset the OBE by delet ng the _M grat onH story, Employees and Departments table.
Summary