Projects
DevMagic Studio provides visual designers for you to rapidly create EF Core entities and enumerations.
The project types such as C# Web API and C# Library that support the visual designers are called "Designer-developed" projects. Using these designer-developed projects, you can automatically generate standard .NET projects that contain controllers, services, and C# models. It is possible to open and maintain the auto-generated .NET projects in any .NET development environment (for example, Visual Studio).
There are two development modes for the designer-developed projects:
- Code First (creating the entities/models first and then generating the database from entities)
- Database First (creating the database first and then generating entities from this database)
Project types and work items
When you create a designer-developed project in DevMagic Studio, you can select from the following project types in the Designer-developed tab:
Web API: Create a design project from which the back-end Web APIs can be generated.
Library: Create an empty library.
Right click on the project node, and then select the menu Add > New Item. In the Designer-developed tab, you can select to create the following items in a visual designer:
- Entity Design (from Scratch): Create an empty entity metadata file.
- Entity Design (from Database): Create an entity metafile by importing the database schema from an existing database.
- Base Entity Design: Create a base entity which can be inherited by the other entities.
- Owned Entity Design: Create an empty owned entity metadata file.
- Enum Design: Create an enumerator metadata file.
Web API project
Content of this article
This article briefly introduces how to create and use a Web API project in DevMagic Studio.
Overview
In DevMagic Studio, the Web API project is a designer project that can generate the ASP.NET Core Web API project and code; you can create a Web API project through New Project -> Designer-developed -> Web API.
The Web API project can not only create the design file for EF core entities and enumerators, but also configure for the C# code generation and database connection.
In the Web API project, you can reference the other projects (such as a Library project), by right-clicking Dependencies under the designer project, selecting Add Project Reference, and then selecting the project you want to reference, as follows:
Database connections
You can add a database connection in the Web API project.
To do that, right click Connections under the project, and then select New connection....
In the New Connection window that pops up, specify the database connection settings.
After specifying the required settings, click OK to save.
You can select the database connection later when generating the code.
Generation profiles
You can add a generation profile in the Web API project.
To do that, right-click GenerationProfiles under the project, and then select New generation profile....
In the profile window that displays, specify the new generation configuration.
Multiple generation profiles can be created. Each can have its own configurations and can be used to generate code differently.
Library project
Content of this article
This article briefly introduces the creation and use of Library projects in DevMagic Studio.
Overview
The Library project is a class library project that can be created through New Project -> Designer-developed -> Library.
Like the Web API project, the Library project can create the design file for EF core entities and enumerators, but it has no generation profile, so it cannot generate code, but it can be referenced by the Web API projects.
To reference a Library project, you can right click Dependencies under the designer project, select Add Project Reference, and then select the Library project you want to reference, as follows:
Library inheritance
After referencing the Library project, the entities and enumerations in the Library project can be used in the current project. For example, when a project named DesignerProject inherits a Library project containing User.dmedm and Gender.dme, User.dmedm and Gender.dme can be used in the entities of the DesignerProject project. As follows, User.dmedm can be referenced in entities.
The enumeration (Gender.dme) in the Library project can also be used directly in the Filter Design and Expression designer. As follows, use Gender.dme in the Expression designer.
Similarly, the authorization policy added to the Library project will also apply to the current project. For example, add a custom authorization policy Policy to the Library project.
This authorization policy can also be used in the DesignerProject project, but it cannot be modified or deleted. It can only be modified and deleted in the Library project.
In addition, after referencing the Library project, its custom code can also be synchronized to the current project. You need to select Apply referenced projects custom code settings on the Custom Code page of the current project, as follows:
The custom code added to the Library project is as follows:
The added custom code will also be synchronized to the DesignerProject project, again the custom code synchronized from the Library project cannot be modified or deleted, as follows:
Base entity
Content of this article
Overview
When multiple entities have the same fields, to avoid the repeated design of the same fields, DevMagic Studio provides Base Entity. Base Entity is equivalent to a simplified Entity Design file with only Field Design and Code Preview. It can be used to store the same fields of multiple entities. It cannot generate interfaces, etc., and can only be used after it is inherited by other entities, and Base Entity can only inherit Base Entity.
Examples
Now, we will use an example to illustrate how to create and use a base entity.
When multiple entities have fields such as primary key, creator, creation time, updater, and update time, we can create a Base Entity file and design the fields in Field Design as follows:
If an entity needs these fields, just select that Base Entity file in the entity, and the entity will inherit the primary key, creator, creation time, updater, and update time fields, but the inherited fields are read-only, as follows:
For entities that inherit from Base Entity, except that the inherited fields are read-only, the behavior of the inherited fields is consistent with that of the directly added fields. These fields will also be included in the database table after projects are generated and run, as follows:
Owned entity
Content of this article
Overview
In EF Core, an owned entity type is an entity type that can only appear on the navigation properties of other entity types, and the entity containing the owned entity type is its owner.
DevMagic Studio provides Owned Entity for users to store the fields of owned entity types. An Owned Entity has only Field Design and Code Preview, and cannot set primary keys, or inherit from or be inherited by other entities, but it can be referenced by other entities.
One to one
When an owned type always has a one-to-one relationship with an owner, for example, one user has one working address, we can achieve this using an Owned Entity.
First, you create an Owned Entity (Address.dmedm) and add Street, City, and State fields to this entity.
Next, you add a field (WorkingAddress) in the user entity (User.dmedm) and reference to the owned entity (Address.dmedm).
Generate and then run the project. A User table will be generated in the database, and it contains three fields of the owned entity, as follows:
If you add two working addresses, the data will be stored in the User table through the three fields: WorkingAddress_Street, WorkingAddress_City and WorkingAddress_State, as follows:
One to many
When the owner and subordinate entity type is a one-to-many relationship, for example, one user has multiple delivery addresses, the one-to-many relationship can also be implemented using Owned Entity, and DevMagic Studio will convert the user's multiple delivery addresses through the converter and store addresses in a field in JSON format.
First, you will need to use an Owned Entity, and the same Owned Entity (Address.dmedm) is used here.
Next, you add a field (ShippingAddress) in User.dmedm and reference to Address.dmedm and select Collection. User.dmedm is designed as follows:
After generating and running the project, a User table is generated in the database, and it contains a ShippingAddress field, as follows:
If you add two pieces of data, the data will be stored in the ShippingAddress field in JSON format and multiple addresses will be stored in one string (collection), as follows: