This tutorial shows how to use Entity Framework Core to perform operations in .NET applications. With dotConnect for Oracle, we'll create EF Core models using Scaffold-DbContext and Entity Developer, build a console application, and implement CRUD operations using a robust connection class.

Why dotConnect for Oracle?

ORM support provided

Advanced ORM support

Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.

Full ADO.NET compliance

Full ADO.NET compliance

Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.

Support for MySQL-specific data types

Oracle-specific data types

Offers many Oracle-specific features and fully supports all unique data types for accurate and complete data representation.

Secure connection ensured

Secure connection options

Provides robust security with support for SSL/SSH connections, proxy authentication, and OS authentication (NTLM).

Integration with popular IDEs

IDE integration

Features native integration with Visual Studio and complete design-time support for accelerated development.

Priority support provided

Priority support & frequent updates

Includes priority support, detailed documentation, and regular updates for continuous improvement.

Download and activate dotConnect for Oracle

You can start using dotConnect for Oracle immediately with a 30-day free trial. Choose one of the following installation options:

30-day free trial version

dotnet add package Devart.Data.Oracle
Install-Package Devart.Data.Oracle

You can install the driver by using the Windows installer.

After you receive the license key, add it to your connection strings to connect to the data source.

Start using dotConnect for Oracle in your project today with a free trial

Select Oracle database objects

Using the built-in Data Explorer, configure the connection to Oracle and then select the objects you want to use in your project.

1. In Visual Studio, click Tools on the menu bar and select Connect to Database from the drop-down list.

DbContext class

2. In the Add Connection window, choose the Oracle data source, fill in the connection details for your Oracle database, and click OK to proceed.

DbContext class

In Server Explorer, select the tables and fields you want to use. In our tutorial, we will use the EMPLOYEES table.

DbContext class

Create an EF Core model from the database

Once you have configured the database, you can move on to the next step—creating an EF Core model. You can do this in two ways: using Scaffold-DbContext or via Entity Developer.

Create an EF Core model via Entity Developer

Entity Developer allows you to visually design and generate EF Core models, making database application development faster, easier, and more efficient. If you don't have it already installed, close your Visual Studio instance and download Entity Developer. Install it on your machine following the on-screen instructions.

Follow the detailed illustrated guide to create your database model using Entity Developer. When this process is complete, the model you created opens, so you can work with it.

DataModel file opened in Visual Studio

It also creates the DbContext class:

DbContext class

Create an EF Core model using Scaffold-DbContext

You can use Scaffold-DbContext to generate DbContext and entity classes for your Oracle database. Run the following command, replacing values with your actual credentials:

Scaffold-DbContext "DataSource=127.0.0.1;Port=1521;SID=XE;UserId=TestUser;Password=TestPassword;LicenseKey=**********;" -provider Devart.Data.Oracle.Entity.EFCore -OutputDir Models

After you execute this command, the ModelContext file and the Models folder containing the table entity classes are generated.

Direct mode options properties

Property Meaning
Direct Property indicating whether direct mode is to be used
Host IP address or hostname of the Oracle server
Port Port number for the Oracle server
ServiceName Service name for the database instance
SID Oracle System Identifier (SID) for the database instance
UserId Oracle database username
Password Password associated with the Oracle database username
LicenseKey License key (only required when using .NET Standard compatible assemblies)

Connect to Oracle and retrieve data

This section shows how to connect to an Oracle database and retrieve the first 10 rows from the EMPLOYEES table using EF Core. We'll query the data and display it in the console for verification.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var context = new DataModel())
            {
                var employees = context.EMPLOYEEs
                    .Take(10)
                    .ToList();

                Console.WriteLine("First 10 Employees:");
                Console.WriteLine("------------------");
                foreach (var employee in employees)
                {
                    Console.WriteLine($"ID: {employee.EMPLOYEEID}, Name: {employee.FIRSTNAME} {employee.LASTNAME}, Email: {employee.EMAIL}, Hire Date: {employee.HIREDATE:yyyy-MM-dd}, Salary: {employee.SALARY:C}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Run the application. If everything is set up correctly, the console will display the first 10 records from the EMPLOYEES table.

Connect to Oracle and retrieve data

Insert new records using EF Core

Here, we insert a new employee record into the EMPLOYEES table. EF Core's Add method is used to stage the record, and SaveChanges persists it to the database. The console displays the inserted record for debugging.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var context = new DataModel())
            {
                var newEmployee = new EMPLOYEE
                {
                    FIRSTNAME = "John",
                    LASTNAME = "Doe",
                    EMAIL = "[email protected]",
                    HIREDATE = DateTime.Now,
                    SALARY = 75000.00
                };

                context.EMPLOYEEs.Add(newEmployee);
                context.SaveChanges();
                
                Console.WriteLine($"New employee added with ID: {newEmployee.EMPLOYEEID}");
                Console.WriteLine();

                var employees = context.EMPLOYEEs
                    .Take(10)
                    .ToList();

                Console.WriteLine("First 10 Employees:");
                Console.WriteLine("------------------");
                foreach (var employee in employees)
                {
                    Console.WriteLine($"ID: {employee.EMPLOYEEID}, Name: {employee.FIRSTNAME} {employee.LASTNAME}, Email: {employee.EMAIL}, Hire Date: {employee.HIREDATE:yyyy-MM-dd}, Salary: {employee.SALARY:C}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

Sample output:

Insert New Records Using EF Core

Update Oracle data using EF Core

This section shows how to update the record with EMPLOYEEID 41 by changing the employee’s first and last name. We retrieve the record, modify the properties, and call SaveChanges to persist the changes.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var context = new DataModel())
            {
                var employeeToUpdate = context.EMPLOYEEs.FirstOrDefault(e => e.EMPLOYEEID == 41);
                if (employeeToUpdate != null)
                {
                    Console.WriteLine($"Before update - ID: {employeeToUpdate.EMPLOYEEID}, Name: {employeeToUpdate.FIRSTNAME} {employeeToUpdate.LASTNAME}");
                    
                    employeeToUpdate.FIRSTNAME = "UpdatedFirst";
                    employeeToUpdate.LASTNAME = "UpdatedLast";
                    context.SaveChanges();
                    
                    Console.WriteLine($"After update - ID: {employeeToUpdate.EMPLOYEEID}, Name: {employeeToUpdate.FIRSTNAME} {employeeToUpdate.LASTNAME}");
                }
                else
                {
                    Console.WriteLine("Employee with ID 41 not found.");
                }
                Console.WriteLine();

                var employees = context.EMPLOYEEs
                    .ToList();

                Console.WriteLine("All Employees:");
                Console.WriteLine("--------------");
                foreach (var employee in employees)
                {
                    Console.WriteLine($"ID: {employee.EMPLOYEEID}, Name: {employee.FIRSTNAME} {employee.LASTNAME}, Email: {employee.EMAIL}, Hire Date: {employee.HIREDATE:yyyy-MM-dd}, Salary: {employee.SALARY:C}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

The console shows the updated record for verification:

Update Oracle Data Using EF Core

Delete Oracle data using EF Core

Finally, we delete the employee with EMPLOYEEID 41 from the EMPLOYEES table. The record is removed using the Remove method, and SaveChanges commits the deletion.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (var context = new DataModel())
            {
                var employeeToDelete = context.EMPLOYEEs.FirstOrDefault(e => e.EMPLOYEEID == 41);
                if (employeeToDelete != null)
                {
                    context.EMPLOYEEs.Remove(employeeToDelete);
                    context.SaveChanges();
                }

                var employees = context.EMPLOYEEs.ToList();

                foreach (var employee in employees)
                {
                    Console.WriteLine($"ID: {employee.EMPLOYEEID}, Name: {employee.FIRSTNAME} {employee.LASTNAME}, Email: {employee.EMAIL}, Hire Date: {employee.HIREDATE:yyyy-MM-dd}, Salary: {employee.SALARY:C}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}

The console confirms the deletion:

Delete Oracle Data Using EF Core

Video tutorial: Connect to Oracle with EF Core

Conclusion

In this tutorial, we've demonstrated how the integration of Entity Framework Core with Oracle using dotConnect for Oracle streamlines data operations in .NET applications. The rich features of dotConnect for Oracle, combined with the simplicity of Entity Framework Core, provide a powerful and efficient way to handle database interactions in Oracle-powered applications. With these tools and techniques, you can confidently build scalable and maintainable .NET solutions that leverage Oracle databases.

FAQ

How do you install and activate dotConnect for Oracle in a .NET project?
Install dotConnect for Oracle via the EXE installer or by adding the Devart.Data.Oracle NuGet package to your project, then obtain your personal activation key from your Devart Customer Portal and include it in the connection string via the License Key parameter for a working connection.
How do you create a connection to Oracle using dotConnect in C#?
Define a connection string that includes host, user ID, password, database, and the License Key value, then create an OracleConnection instance with this string and call Open() for it inside a try-catch block to test and handle connection errors.
How do you enable encryption for secure Oracle connections with dotConnect?
Use Oracle Native Network Encryption by configuring encryption in the sqlnet.ora file on both client and server sides. No special parameters are required in the connection string.
Can you connect to Oracle using Entity Framework Core and dotConnect?
Yes, you can either use Entity Developer to visually create an EF Core model from the database or run Scaffold-DbContext with a dotConnect connection string (including License Key) to generate the DbContext and entity classes.
Is it possible to connect to Oracle using Visual Studio Server Explorer with dotConnect?
Yes, it is possible. All you need to do is navigate to Visual Studio Server Explorer, add a new Data Connection, choose dotConnect for Oracle as the data provider, enter your credentials, test the connection, and then browse Oracle data directly in Visual Studio.

Dereck Mushingairi

I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.

dotConnect for Oracle

Get an enhanced ORM-enabled data provider for Oracle and develop .NET applications working with Oracle data quickly and easily!

Try the 30-day trial of the full product. No limits. No card required. Start free trial