Skip to main content

How You Can Create a .NET Core Application Using Entity Framework Core with Oracle

While Entity Framework support is already a quite common feature of different data provider products, support for Entity Framework Core is not yet as widely available. Especially if it concerns Entity Framework Core on .NET Core. Today I will tell you about an ADO.NET provider for Oracle that provides such features – dotConnect for Oracle. dotConnect for Oracle is a product of Devart company with a many-years history of development, support for Oracle versions, starting from 7.3 to the latest 18c. In this post, we’ll see how you can create a .NET Core application using Entity Framework Core with Oracle.

General dotConnect for Oracle Features

dotConnect for Oracle is a high-performance ADO.NET provider with a wide support of Oracle-specific features and technologies, such as Direct Path loading, Advanced Queuing, SQL and PL/SQL tracing, change notifications, transaction guard, and many others.

One of the essential features of dotConnect for Oracle is support for Direct mode connections – ability to connect to Oracle Server without Oracle Client software. This a powerful feature, giving dotConnect for Oracle the ability to work on systems, where installation of Oracle Client is not desirable or even not possible.

.NET Framework Compatibility

dotConnect for Oracle supports various .NET Frameworks and their versions. It supports Full .NET Framework, .NET Compact Framework 2.0 and above, Mono version 2.0 and above, and of course .NET Core 1.x and 2.x.

Users should note that if they develop projects for Full .NET Framework or Mono, they should download dotConnect for Oracle installer and install the product. However, to develop applications, targeting .NET Core or .NET Standard, they need to install the corresponding NuGet packages.

ORM Support

The main feature of dotConnect for Oracle is support for different ORM solutions. dotConnect for Oracle supports NHibernate – a well-known open-source ORM. It also supports LinqConnect – Devart’s own ORM solution, highly compatible with LINQ to SQL, but extending its feature set with new functionality. And of course dotConnect for Oracle supports all the release Entity Framework versions – Entity Framework v1 – v6 and Entity Framework Core 1.1 and 2.1.

For Entity Framework, dotConnect for Oracle offers high performance because of Batch Updates, wide support for LINQ to Entities, stored procedures and functions support, high configurability, etc. Moreover, dotConnect for Oracle is supplied with a visual ORM model designer – Entity Developer. It seamlessly integrates into Visual Studio, supports model-first, database-first, or mixed approaches to development, allows testing the model with LINQ Queries, supports integration with Visual Studio refactoring and provides its own Model Refactoring feature. Entity Developer uses highly customizable T4 template based code generation, which allows even custom templates to generate code.

Create a .NET Core Application Using Entity Framework Core with Oracle

You can use Entity Framework Core functionality of dotConnect for Oracle both in applications, targeting Full .NET Framework and applications, targeting .NET Core. Here is how you can create an application, using Entity Framework Core.

Create a .NET Core Application Using Entity Framework Core

For this example, we will use a simple console application, using an Entity Framework Core model, created with Database First approach, using sample tables, created in dotConnect for Oracle tutorials.

Prerequisites

Even though we will use dotConnect for Oracle assemblies from NuGet packages, if we want to use Entity Developer for designing the model, we need to install dotConnect for Oracle.

Besides dotConnect for Oracle, you will need Visual Studio 2017 with all the necessary updates, installed with the .NET Core cross-platform development tool set selected.

Creating New Project

  • Open Visual Studio 2017
  • On the File menu point to New and then click Project. The New Project dialog box will open.
  • On the left side of the dialog box select Installed -> Visual C# -> .NET Core.
  • On the left side of the dialog box select Console App.

    Create a .NET Core Application Using Entity Framework Core with Oracle

  • Enter the project name and location if necessary.
  • Click OK. A new project will be created.

Creating an Entity Framework Core Model

  • Right-click the project in Solution Explorer.
  • Click Add -> New Item on the shortcut menu.
  • On the left side of the dialog box select Visual C# -> ASP.NET Core -> Data.
  • On the left side of the dialog box select Devart EF Core Model.

    Creating a .NET Core Application Using Entity Framework Core

  • Click Add. Entity Developer Create Model Wizard will open.
  • In our example, we create a model from a database. Select Database First and click Next.

    Creating a .NET Core Application Using Entity Framework Core

  • In the Provider box, select dotConnect for Oracle.
  • Specify the necessary connection settings. If you want to use a direct connection (without Oracle Client), you need to select the Direct check box and specify SID and port instead of Oracle Home. Click Next.

    Creating a .NET Core Application Using Entity Framework Core

  • Click Next again.
  • Select the necessary tables to add to the model and click Next.
    Creating a .NET Core Application Using Entity Framework Core
  • On the next page you can configure naming rules for generated classes and properties. Click Next.
    Creating a .NET Core Application Using Entity Framework Core
  • On this page you can configure model settings. Click Next.
  • On the next page you can select entities to add to the default model diagram. For a more convenient work with large models, Entity Developer allows multiple diagrams for a model to group entities. In our example, we create just a small model, so we add all the entities to the diagram.
  • Finally, on the next page, you can select templates that will be used for code generation and configure their settings. By default, one EF Core template is selected, which is used to generate classes for the EF Core model. You can add more templates, for example, for adding Data Annotation Metadata classes, Data Transfer Object classes, etc. Click Next, and then click Finish.

image5

Our model is ready. You can edit it in Entity Developer and then sync model changes to database tables, or we can modify the database and then sync changes to the model.

Note that you can also use standard Entity Framework code generation feature via Scaffold-DbСontext command with dotConnect for Oracle. In this case you need to install the necessary NuGet packages (Microsoft.EntityFrameworkCore.Tools and Devart.Data.Oracle.EFCore) manually And run this command, passing the connection string and the provider as parameters. This can be a faster way to generate code, but it provides far less features for model editing and code generation customization than using Entity Developer.

dotConnect for Oracle Technical Licensing

For .NET Core/.NET standard projects, dotConnect for Oracle requires the license key specified. If you purchased dotConnect for Oracle, you have received the license key, which should be specified as the license key connection string parameter. For the trial version, you use the trial license key files, generated when installing dotConnect for Oracle.

Trial license key files are generated in the ProgramData\Devart\dotConnect\License\ folder, and this is where dotConnect for Oracle NuGet assemblies look for them by default. If you need to place them to another folder or use them on non-Windows computers, you need to specify the path to them in the license key connection string parameter in the following way:

license key=trial:/home/test/Devart.Data.Oracle.key

The path is case-sensitive, and must be specified exactly in the same case as it is in your file system.

Using Model Classes

For our simple example we will just use the model as is, and just add code to get data from one of the entities. So let’s add the following code to the Main function:

using (var db = new demobaseModel())
{
   // Creating a new department and saving it to the database
   var newDept = new DEPT();
   newDept.DEPTNO = 60;
   newDept.DNAME = "DEVELOPMENT";
   newDept.LOC = "HOUSTON";
   db.DEPTs.Add(newDept);
   var count = db.SaveChanges();
   Console.WriteLine("{0} records saved to database", count);

   // Retrieving and displaying data
   Console.WriteLine();
   Console.WriteLine("All departments in the database:");
   foreach (var dept in db.DEPTs)
   {
      Console.WriteLine("{0} | {1}", dept.DNAME, dept.LOC);
   }
   Console.ReadLine();
}

This code adds a new record to the table, selects data from it, and displays it in the console.

Create a .NET Core Application Using Entity Framework Core with Oracle

Conclusion

In this post we have discussed features of dotConnect for Oracle – an ADO.NET provider with Entity Framework and Entity Framework Core support, and demonstrated using this support in a simple .NET Core application.

If you are interested, you may try dotConnect for Oracle yourself. The trial installer can be downloaded from https://www.devart.com/dotconnect/oracle/download.html and NuGet packages are installed from NuGet as usual. dotConnect for Oracle provides a 30-day trial period with unrestricted functionality, so you can freely try the product.

If you want to use Entity Framework Core for other databases on .NET Core, Devart also has providers with support for it for MySQL, PostgreSQL, and SQLite. Devart also has a number of ADO.NET providers with Entity Framework Core support for other databases and cloud applications, but they don’t support .NET Core yet.

This post is written by Sergey Bykov.

PS: If you found this content valuable and want to return the favour, then Buy Me A Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *