How to Create and Configure an EF Core Model Using Visual ORM Designer
In this tutorial, you'll get acquainted with Entity Developer and learn how to make the most of it by configuring and creating an EF Core model. You'll also explore how it simplifies the development process through visual design and easy integration with your database.
Prerequisites
Here is a list of applications and tools used in this tutorial to demonstrate how to configure and create an EF Core model:
-
IDE: Any integrated development environment you prefer
-
Entity Developer: A standalone tool used to generate EF Core models
-
Data provider: dotConnect or any open-source package. In this tutorial, we will use the open-source MySql.Data core package.
-
Test database: The sakila database used in the tutorial
-
Test project: A .NET console application used to demonstrate model functionality
-
Entity Framework Core: A powerful cross-platform Object-Relational Mapping (ORM) tool developed by Microsoft for .NET developers
Why Entity Developer?
Entity Developer is a powerful modeling and code generation tool designed to streamline development with .NET ORM frameworks like Entity Framework Core, LinqConnect, NHibernate, and Entity Framework.
A standalone application vs. a Visual Studio-integrated version
Entity Developer is available both as a Visual Studio add-in and as a standalone application, giving you flexibility depending on your development preferences.
In this tutorial, we will show you how to configure and create an EF Core model using the standalone version.
What is an EF Core model?
An EF Core model represents the structure of your application’s data, including entity classes, their properties, and relationships. It acts as a bridge between the database and .NET code, allowing developers to interact with data through objects rather than writing SQL queries. You can create an EF Core model using either the Database-First approach (from an existing database) or the Model-First approach (designing the Model-First and generating the database), and tools like Entity Developer support both methods for a streamlined experience.
Database-First and Model-First approaches
Entity Framework Core offers developers two primary approaches to developing data models and databases: Database-First and Model-First. The Database-First approach is ideal when working with an existing database, allowing developers to reverse-engineer the model and quickly generate code from the database schema. This method is very popular among developers who prioritize database design and want to work with an existing database structure.
Meanwhile, the essential concept of the Model-First approach is defining the data model first and then generating both the database schema and the corresponding entity classes from that model. This approach is beneficial if you want to focus on designing your data model first and have EF Core automatically generate the necessary code and database schema for you.
Entity Developer works with both approaches, offering a visual interface to design models without writing code. It simplifies EF Core development through flexible tools and seamless database integration.
In this tutorial, you’ll learn how to create an EF Core model using both methods.
The Database-First approach
In this section, you will learn how to create and update models using the Database-First approach in Entity Developer, our standalone modeling and code generation tool.
Create an EF Core model
1. Launch Entity Developer.
2. On the toolbar, select File > New Model.
3. In the New Model dialog that appears, choose EF Core Model and click Create.
4. On the Create Model Wizard page, select Database First and click Next.
5. Next, select a data provider. In this case, choose the MySql.Data open-source connector.
6. Enter the connection details. Then, click Test Connection to verify the connection is successful. Once confirmed, click Next.
7. Specify whether you want to create your model based on an existing database or start from scratch. In this tutorial, we will generate it from a database. Click Next to continue.
8. Choose the database objects you want to scaffold. Then, click Next.
9. Define the naming convention for the property names in the database object and click Next. We suggest keeping the default settings this time.
10. On the Model properties page, configure your model settings. Here, you can select the target EF Core version compatible with your project’s .NET Framework and the Target .NET Framework version. Once completed, click Next.
11. Choose the model diagram content. You can use all entities, split the entities by database, or do a custom selection. For this tutorial, select All Entities and click Next.
12. Choose code generation templates for your model. You can define different parameters you want the model to follow. Let's use the default settings for this tutorial. Click Next.
Your model is ready now. Finally, click Finish to close the wizard.
Once the model is created, it opens in Entity Developer. Before working with it, consider the following steps:
1. Save the model to the project directory.
2. Next, generate code and dbcontext classes. For this, select Model > Generate Code.
You will see the code generation in progress.
When the code generation is complete, review the generated model classes.
Update the model
1. Right-click the created model and select Add > New Property.
2. In the Property Editor dialog, define property settings and click OK to save your changes.
3. After defining the settings, update the database to apply the latest changes. Select Model > Update Database From Model.
4. In the Update To Database Wizard, ensure the Recreate Database Table and Regenerate Storage options are unchecked and click Next.
In addition to updating the database, the wizard also allows you to completely regenerate it or update only the storage part of the model. Simply select the appropriate option on the wizard page and follow the on-screen instructions.
5. Select the database schema(s) that you want to update. In our case, we are adding the sakila database. Click Next to proceed.
6. Review the list of tables that will be scripted and uncheck any tables you do not want to include. Click Next to continue.
7. Review the action plan to make sure it meets your requirements. Click Execute to run the script.
8. Once the script executes successfully, click Finish to close the wizard.
9. Save the model to the project directory and verify that the new property appears in the generated code.
The Model-First approach
This section will guide you through creating and updating models using the Model-First approach in Entity Developer, our dedicated tool for modeling and code generation.
Create an EF Core model
1. Launch Entity Developer.
2. On the toolbar, select File > New Model.
3. In the New Model dialog that appears, choose EF Core Model and click Create.
4. On the Create Model Wizard page, select Model First and click Next.
5. On the Model properties page, configure your model settings. Here, you can select the target EF Core version compatible with your project’s .NET Framework and the Target .NET Framework version. Once completed, click Next.
6. Choose code generation templates for your model. You can also define specific parameters to customize how the model is generated. Let’s stick to the default settings for this tutorial. Click Next.
The model is ready now. Click Finish to close the wizard.
Finally, you can see the created model in Entity Developer.
Add classes and properties
1. Ensure the DataModel is open.
2. Right-click your model and select Add > New Class.
3. In the Class Editor dialog, type the class name in the Name field and click OK. For this tutorial, use Customers as the class name.
The created class then opens in the model.
After creating a class in Entity Developer, you can define its properties:
1. Right-click the class and select Add > New property.
2. In the Property Editor dialog, configure the class properties as needed, and click OK to save the property settings. In our example, the properties are defined as follows:
- Name: Id
- Type: Int32
- Value Generated: Never
- Primary key: Select the checkbox to indicate that this class includes a primary key
Add associations
Assume you’ve created two classes and want to define an association between them. In our example, we’ll create an association between the Customers and Orders classes, where the Customers' primary key (ID) is referenced by the Orders class.
To create the association:
1. Right-click Customers in the model and select Add > New Association from the menu.
2. In the Association Editor dialog that appears, configure the required properties and click OK.
- Cardinality: Set to OneToMany, as each Customer has multiple Orders
- On Delete Action: Set to NoAction
Once added, the association will be displayed in the model diagram.
Add inheritances
You can extend the current model by adding the third PremiumCustomer class, which will be configured as a derived class from the previously created Customers class. For this, follow the steps below:
1. Create the PremiumCustomer class and configure its properties.
2. Right-click the model and select Add > New Inheritance from the context menu.
3. In the Inheritance Editor dialog, configure an inheritance class and set the discriminator column, then click OK. Let's configure the following properties:
- Base Class: Customers
- Derived Class: PremiumCustomer
- Type: Table Per Hierarchy
- Column: IdDescriminator
- Type: Int32
As a result, PremiumCustomer becomes a subclass of Customers, adding inheritance to the model.
4. Now, save the model. Once saved, generate the code by selecting Model > Generate Code on the toolbar.
Generate an SQL database script
With Entity Developer, you can generate database scripts from any EF Core model, whether created using the Database-First or Model-First approach.
1. Ensure the model is all set.
2. On the toolbar, select Model > Generate Database Script From Model.
3. On the Generate Database Script Wizard page that appears, leave the Include Drop and Regenerate Storage checkboxes cleared and click Next to proceed.
4. Configure the model synchronization settings and click Next.
5. Review the list of tables that will be scripted and clear the checkboxes next to the tables you do not want to include. Click Next to continue.
Now, review the script to ensure it is successfully generated. Click Finish to close the wizard.
Benefits of using Entity Developer
Entity Developer is an enterprise-grade tool that makes ORM design faster, more efficient, and error-free. It's an indispensable, intuitive solution for developers of all skill levels — and here’s why:
- Accelerated Development Workflow: Keep your model and database in sync with automatic update and code regeneration features
- Comprehensive ORM Support: Work with EF Core, Entity Framework, LinqConnect, and NHibernate — all within a single tool
- Seamless Visual Studio Integration: Fully integrated with Visual Studio for a smooth development experience
- Broad Compatibility: Compatible with major relational databases like SQL Server, Oracle, MySQL, PostgreSQL, and SQLite
Conclusion
This tutorial gives detailed instructions on how to create and update models using the Database-First and Model-First methods in Devart Entity Developer. The implementation of the Model-First method means that the developer defines the Model-First, and EF generates a database using this model. Meanwhile, in the Database-First approach, the model is generated using an existing database.
Both approaches are thoroughly explained in this tutorial, with detailed code snippets, illustrations, screenshots, and additional resources.