Generating Model From Oracle Database using Entity Developer

This tutorial shows how to create a .NET Core Application using Entity Framework Core for an Oracle database. It uses dotConnect for Oracle, a high-performance ADO.NET provider from Devart. The tutorial demonstrates Database-First approach to create an Entity Framework Core model and generate the corresponding code.

Entity Framework Core implements Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command allows quick generation of a DbContext and entity type classes for a specified database, but it doesn't provide much options for customization.

In this tutorial, we will show Database-First approach using our dedicated ORM designer and code generation tool - Entity Developer. It supports Database-First, Model-First, or mixed developer approaches and allows you to quickly generate a model from a database, tweak it visually, automatically apply model changes to a database, if necessary, and generate code. Entity Developer seamlessly integrates into Visual Studio, allows testing the model with LINQ Queries at design-time, supports integration with Visual Studio refactoring and provides its own Model Refactoring feature.

Note: You can find a similar tutorial, using the Scaffold-DbContext command to generate code here.

This tutorial shows how to create a simple console application, powered by Entity Framework Core and using Database-First approach. It uses the tables, we have created in the Creating Database Objects and Inserting Data Into Tables tutorials.

Download Now dotConnect for Oracle

Requirements

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 also install dotConnect for Oracle from its installer.

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

Creating New Project

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

    New Project dialog box

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

Creating an Entity Framework Core Model

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

  4. On the left side of the dialog box select Devart EF Core Model.

    Add New Item dialog box

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

    Selecting approach

  7. In the Provider box, select dotConnect for Oracle.
  8. 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.

    Connection settings

  9. Click Next again.
  10. Select the necessary tables to add to the model and click Next.

    Selecting tables

  11. On the next page you can configure naming rules for generated classes and properties. Click Next.

    Naming rules

  12. On this page you can configure model settings. Click Next.
  13. 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.
  14. 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.

    Code generation templates

The 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.

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:

Using Model

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:

[C#]
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.

Program execution result

Conclusion

This article shows how to create a .NET Core application, working with an Oracle Database via Entity Framework Core, using dotConnect for Oracle as an Entity Framework Core provider. dotConnect for Oracle is an ADO.NET provider from Devart with support for such ORM solutions as Entity Framework v1 - v6, Entity Framework Core, NHibernate, and Devart's own ORM LinqConnect.

This tutorial uses Entity Developer to generate context and entity classes. Entity Developer is a powerful visual ORM model designer with support for Database First, Model First, and mixed development approaches and powerful code generation.


Back to list

dotConnect for Oracle

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