Scaffolding Or Reverse Engineering

ODP.NET EF Core supports scaffolding the following tables and views:

  • Relational tables and views

  • Materialized views

By convention, ODP.NET EF Core maps an appropriate .NET data type based on the Oracle Database data type and its characteristics.

When scaffolding, developers may wish to change the default data type mappings between Oracle Database and .NET. When modifying .NET CLR types of the generated entity's property, remove the auto-generated HasColumnType() Fluent API in the model. For example, an Oracle NUMBER(4) is scaffolded to a .NET Byte data type by default. If the .NET CLR data type is changed to .NET Short or Int16 data type, then an invalid cast exception at runtime may occur until the HasColumnType("NUMBER(4)") has been removed.

Data Type Mapping

This table shows the default mappings.

Table 5-2 ODP.NET Entity Framework Core Reverse Engineering Data Type Default Mappings

Oracle Database Data Type .NET Type Alias .NET Data Type

NUMBER(1)

bool

System.Boolean

NUMBER(2) to NUMBER(4)

byte

System.Byte

NUMBER(5)

short/int16

System.Int16

NUMBER(6) to NUMBER(10)

int/int32

System.Int32

NUMBER(11) to NUMBER(19)

long/int64

System.Int64

NUMBER(>19)

decimal

System.Decimal

NUMBER(p,s)

decimal

System.Decimal

NUMBER

decimal

System.Decimal

BINARY_FLOAT

float

System.Float

BINARY_DOUBLE

double

System.Double

BOOLEAN

bool

System.Boolean

TIMESTAMP

DateTime

System.DateTime

TIMESTAMP WITH TIMEZONE

DateTimeOffset

System.DateTimeOffset

TIMESTAMP WITH LOCAL TIMEZONE

DateTimeOffset

System.DateTimeOffset

DATE

DateTime

System.DateTime

INTERVAL DAY TO SECOND

TimeSpan

System.TimeSpan

INTERVAL YEAR TO MONTH

string

System.String

VARCHAR2

string

System.String

JSON

string

System.String

NVARCHAR2

string

System.String

CHAR

string

System.String

NCHAR

string

System.String

CLOB

string

System.String

NCLOB

string

System.String

RAW

byte[]

System.Byte[]

BLOB

byte[]

System.Byte[]

XMLTYPE

string

System.String

ROWID

string

System.String

UROWID

string

System.String

LONG

string

System.String

BFILE

byte[]

System.Byte[]

LONG RAW

byte[]

System.Byte[]

Scaffolding Tables from Another Schema

Developers can scaffold tables from other schemas other than the user/schema they are connected with. The connected user requires privileges to access the other schema's objects. Once these privileges are granted, developers can use the Package Manager Console (PMC) tools for Entity Framework Core to perform the scaffolding operation. For example:

Scaffold-DbContext "User Id=scott;Password=<password>;Data Source=myhost:1521/mydb;" 
Oracle.EntityFrameworkCore -Schemas HR -Tables EMPLOYEES

Developers can use the –Schemas and –Tables parameters to specify which schemas and tables/views to scaffold for an Entity Framework Core model. If connecting with one user/schema to create tables from a second schema, the user must have at least SELECT privileges for that second schema.

The following table displays the ODP.NET Entity Framework Core behavior when the –Schemas and/or –Tables parameter is specified or left as default while scaffolding a pre-existing model using the Package Manager Console command, Scaffold-DbContext. All sample command excerpts below use Scaffold-DbContext syntax. Similar functionality is available using the EF Core tools command, dotnet ef dbcontext scaffold.

Table 5-3 Schema and Table Filter Mapping

Mapping of Schema and Table Filters No Schema Filter Schema Filter

No Table Filter

Generates all tables/views within current user/schema

Box 1

Generates all tables/views in specified user(s)/schema(s)

Box 2

Table Filter

Generates specified tables/views within current user/schema

Box 3

Generates specified tables/views within current schema and all tables/views in specified user(s)/schema(s)

Filtered results include combined Box 2 and Box 3 results.

It is possible to scaffold tables/views in other schemas by appending the schema name in front: -Tables <schema>.<table/view>

For example, the following snippet would scaffold three tables in three different schemas:

-Tables SCHEMA1.TABLEA, SCHEMA2.TABLEB, SCHEMA3.TABLEC

If the schema or table name contains any special characters, such as a period, then use brackets to delimit the schema and table names when using the –Tables option.

-Tables [SCHEMA1].[TABLEA]

Scaffolding Views

Oracle supports scaffolding database relational and materialized views only. These views can be either read-only or updatable. Use the -Tables parameter to specify the views to scaffold. If the parameter is left blank (default), all the user tables, relational views, and materialized views will be scaffolded.