Oracle .NET Database Samples
repository·master·Indexed 19 days ago
https://github.com/oracle/dotnet-db-samplesCode samples for Oracle Data Provider for .NET (ODP.NET) and other Oracle .NET components. Includes implementations for ODP.NET Core, Managed, and Unmanaged drivers, as well as Oracle Entity Framework Core. Examples cover connectivity to Oracle Autonomous Database, Microsoft Entra ID (Azure AD) token authentication, SSO with service principals, and configuration management using OracleConfiguration, OracleDataSourceCollection, and OracleOnsServerCollection.
What's inside oracle-dotnet-db-samples
- This directory provides access to code samples and tutorials presented during Oracle CloudWorld 2022. The demos cover several key areas of .NET development with Oracle Database, including Entity Framework Core (EF Core) versions 6 and 7, asynchronous vs. synchronous ODP.NET patterns, User-Defined Types (UDT), and advanced features like Binary JSON and Client Initiated Continuous Query Notification (CICQN).
Understand Oracle .NET components
masterOracle provides several free components for .NET developers working with Oracle Database:
- Oracle Data Provider for .NET (ODP.NET): An optimized ADO.NET data access provider. It supports advanced features like AI vector search, JSON-relational duality views, Real Application Clusters (RAC), and self-updating client caches. It is available in three types:
- Core: For .NET (Core) runtimes.
- Managed: For .NET Framework (100% managed code, easy deployment).
- Unmanaged: For .NET Framework.
- Oracle Developer Tools for Visual Studio: A Visual Studio Add-in for application lifecycle management, including PL/SQL debugging and schema comparison.
- Oracle Providers for ASP.NET: A collection of providers for storing ASP.NET website state in an Oracle database.
- Oracle Data Provider for .NET (ODP.NET): An optimized ADO.NET data access provider. It supports advanced features like AI vector search, JSON-relational duality views, Real Application Clusters (RAC), and self-updating client caches. It is available in three types:
Access Oracle Database World 2021 .NET Demos
masterThis directory provides demo code and resources related to the session 'What’s New for .NET and Visual Studio Code Developers in Oracle Database 21c' from Oracle Database World 2021. It includes implementations for ODP.NET with .NET 6, Entity Framework Core 6, User-Defined Types (UDT), and advanced features like Binary JSON and Client Initiated Continuous Query Notification (CICQN).Manage TNS entries with OracleDataSourceCollection
masterTheOracleDataSourceCollectionclass allows you to programmatically add and delete network service name (TNS) entries. This is used by ODP.NET to resolve connection strings that rely on TNS names to connect to an Oracle database.Use Unmanaged ODP.NET in Managed/Core Projects
masterWhile the samples are designed for Managed ODP.NET or ODP.NET Core, you can use unmanaged ODP.NET by making the following changes:
- Incorporate
Oracle.DataAccess.dllinto your project. - Add the unmanaged ODP.NET namespace references:
using Oracle.DataAccess.Client;using Oracle.DataAccess.Types;
- Incorporate
Manage ONS server collections with OracleOnsServerCollection
masterTheOracleOnsServerCollectionclass supports managing a collection of logical servers and their corresponding lists of nodes. This is used for configuring the Oracle Notification Service (ONS) daemons to communicate with remote clients.How to handle View DDL in EF Core
masterEF Core does not provide specific migration APIs for database view DDL (Data Definition Language). To create or modify views as part of your database schema management, you must execute raw SQL. The recommended approach is to use theExecuteSqlRawmethod.Execute Oracle PL/SQL stored procedures with EF Core
masterYou can execute Oracle PL/SQL stored procedures and retrieve result sets in Entity Framework Core using anonymous PL/SQL blocks combined with the
FromSqlextension method.This sample demonstrates two patterns for handling result sets via
REF CURSOR:- Explicitly bound REF Cursor: Requires the
REF CURSORparameter to be explicitly bound to the stored procedure statement. - Implicitly bound REF Cursor: Does not require explicit binding of the
REF CURSORparameter.
Relevant SQL setup files:
return-implicit-ref-cursor-stored-procedure.sql: Creates a procedure returning an implicitly boundREF CURSOR.return-ref-cursor-stored-procedure.sql: Creates a procedure returning an explicitly boundREF CURSOR.
- Explicitly bound REF Cursor: Requires the
Use the OracleConfiguration class to set ODP.NET Core configuration
masterTheOracleConfigurationclass provides a single programming interface for setting ODP.NET Core configuration data. Use this class to manage global or session-specific configuration settings for your Oracle database connections within a .NET application.Register ODP.NET Performance Counters (Oracle 19c and higher)
masterIn Oracle 19c and higher, registration is simplified. All installation methods, including NuGet, include a script specifically for registering ODP.NET counters.Configure ODP.NET EF Core for Oracle Autonomous Database (ADB)
masterTo connect to an Oracle Autonomous Database (ADB) using ODP.NET Entity Framework Core, you must configure the
OracleConfigurationproperties and provide a valid connection string. ADB typically requires TCP with SSL (TCPS) connections, which necessitates using an Oracle wallet to store security credentials.Follow these steps for setup:
- Set TNS Admin Path: Assign the directory containing your
tnsnames.oraandsqlnet.orafiles toOracleConfiguration.TnsAdmin. - Set Wallet Location: Assign the directory containing your ADB wallet files to
OracleConfiguration.WalletLocation. - Configure Connection String: Provide a connection string that includes the
user id,password, anddata source. Thedata sourceshould typically be the ADB TNS name.
In this implementation pattern, these configurations are applied within the
OnConfiguringmethod of yourDbContext.// Conceptual implementation within a DbContext protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { OracleConfiguration.TnsAdmin = "/path/to/tnsnames_and_sqlnet_directory"; OracleConfiguration.WalletLocation = "/path/to/adb_wallet_directory"; optionsBuilder.UseOracle("user id=my_user;password=my_password;data source=my_adb_tns_name"); }- Set TNS Admin Path: Assign the directory containing your
Set up Oracle Entity Framework Core 2.x with Dependency Injection
masterThis sample demonstrates how to integrate ASP.NET Core and Dependency Injection with Oracle Entity Framework Core 2.x.
Note: This sample is specifically for EF Core 2.x and does not support later versions.
To run the application, follow these steps:
- Install the NuGet Package: Add the
Oracle.EntityFrameworkCoreassembly from the NuGet Gallery to your project. - Configure Connection Settings: Open
appsettings.jsonand provide your Oracle database credentials, specifically theUser Id,Password, andData Sourceinformation. - Initialize the Database: Use EF Core migrations to create the required schema (specifically the
Blogstable) and populate it with initial data.
Once these steps are completed, running the application will allow the web page to successfully return results from the Oracle database.
- Install the NuGet Package: Add the