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.
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
- 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.
- 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.
- 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.
- 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.
- Click Next again.
-
Select the necessary tables to add to the model and click Next.
-
On the next page you can configure naming rules for generated classes and properties. Click Next.
- 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.
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.
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