Previous Next
Programmers Guide for .NET Chapter 1 -Generating C# .NET, Database and Persistent Library

Chapter 1 - Generating C# .NET, Database and Persistent Library

DB Visual ARCHITECT (DB-VA) can generate C# .NET code, export database schema (DDL) to database and create the persistent library based on your design in the class diagram and entity relationship diagram. DB-VA will generate a high performance O/R Mapping (ORM) layer library that is readily for you to code and build. The ORM library basically intends to takes most of the relational to object-oriented mapping burden off your shoulder. With generated ORM code and library, you can take the plain C# objects to use in the application and tell the ORM layer to persist the object for you (e.g. ObjectDAO.save(myObject);). This chapter gives you an introduction to DB-VA, describe how to configure database, generate database and C# .NET code step by step.

In this chapter:

Introduction

DB Visual ARCHITECT (DB-VA) provides an easy-to-use environment bridging between object model, data model and relational database. You can use visual modeling for both logical data design and physical data design. It also automates the mapping between object model and data model.

In this chapter, we assume that you know how to use the class diagram and entity relationship diagram to design the model (please refer to the Designer Guide for the design with class diagram and entity relationship diagram in details). Class Diagram and Entity Relationship Diagram will be used in this chapter to demonstrate how to use the -VA to export database schema (DDL) to database and generate C# persistent code.

Figure 1.1 - Mapping between classes and entities

Configuring Database

DB-VA covers most of the databases in the market. You can check the latest supported databases version from http://www.visual-paradigm.com/product/dbva/

  1. Please open flight.vpp or draw the class diagram above and synchronize to ERD.
  2. From the menu, select Tools > Object-Relational Mapping (ORM) > Database Configuration... to open the Database Configuration dialog box.
  3. Figure 1.2 - select Database Configuration
  4. Select .NET in Language in drop down menu, select a database and enter database settings. We will use MySQL database in this example.
  5. Figure 1.3 - Database Configuration dialog
  6. Enter database setting
  7. For Driver, select a .NET Driver. It contains the default Driver Class and Dialect. You can click the drop down button to modify its Driver Class and Dialect.

    Figure 1.4 - The driver configuration

    For Driver and Adapter Driver file, you can click the Driver button to select Download Driver and Adapter, Download, Update, or Default Driver. DB-VA will help you to download the most up-to-date driver and adapter driver according to the Driver field information. You also can select Browse...to select a driver and adapter driver file in your computer.

    Figure 1.5 - The download button

    After downloaded the driver file, <<MySQL Connector/Net 1.0.6>> shown on the Driver file indicates that the .NET driver file is downloaded with the specified version number by DB-VA.

    For the Connection String, It provides the Connection String template for different databases. You need to fill in the information for Connection String to connect database.

    The original template for MySQL Connection URL:

    Server=<host_name>;Database=<database_name>;User ID=<username>;Password=<password>; CharSet=<charset>

    The modified template for MySQL Connection URL:

    Server=localhost;Database=control;User ID=root;
  8. Test the database connection by clicking the Test Connection button
  9. Figure 1.6 - Test Connection button

    If success to connect with database the Connection Successful dialog box will show, otherwise the Connection Exception dialog box will appear.

    Figure 1.7 - The connection successful/failure message
  10. Select a database to be the default database which is the default database connection for generating code and database.
  11. DB-VA allows you to change the default database anytime, which means you can change to use any database when you are developing application. And you do not need to worry about the database-specific details because DB-VA will take care of them for you. You only need to configure the target database as default.

Generating Database

Now you can export the database schema from the Entity Relationship Diagram to the default database.

  1. From the menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Database... to open the Database Code Generation dialog box.
  2. Figure 1.9 - To Generate Database
  3. The dialog box shows the previously configured default database setting.
  4. Figure 1.10 - Database Code Generation dialog
  5. Select Generate Database option which specifies the action for the database. Since this is the first time you export database schema, so you can select the Create Database option. DB-VA allows you to select Create Database, Update Database, Drop and Create Database and Drop Database.
  6. Figure 1.11 - Generate Database options
  7. Select Export to database option allows altering the database immediately after you click the OK button.
  8. Figure 1.12 - Export to database option
  9. Select Generate DDL allows the generation of DDL file.
  10. Figure 1.13 - Generate DDL option
  11. If you used some reserved word (e.g. Order) in your database design, you can choose the Quote SQL Identifier option to avoid the naming problem in your design with the target database. Auto -only quote the detected reserved word. Yes -quote all table or column names. No - don��t quote any word(s)
  12. Figure 1.14 - Quote SQL Identifier options
  13. Click Database Options button to reconfigure the database settings before generating database.
  14. Click OK on the dialog box then DB-VA will export the database schema to the default database and generate the DDL file to the output path.
  15. Figure 1.15 - Generate ORM Code/Database dialog
  16. Check the tables created in the MySQL database.
  17. Figure 1.16 - The tables generate in database
  18. Read the generated DDL file
  19. create table Flight (ID int not null auto_increment, departureTime date, departingAirport varchar(255), arrivingAirport varchar(255), PlaneID int, primary key (ID)) type=InnoDB;

    create table Plane (ID int not null auto_increment, PlaneType varchar(255), maxSpeed double not null, maxDistance double not null, primary key (ID)) type=InnoDB;

    alter table Flight add index FK_Flight_1115 (PlaneID), add constraint FK_Flight_1115 foreign key (PlaneID) references Plane (ID);

Generating C# .NET Code

Now you can export the database schema from the Entity Relationship Diagram to the default database.

  1. From the menu bar, select Tools > Object-Relational Mapping (ORM) > Generate Database... to open the Database Code Generation dialog box.
  2. Figure 1.17 - To generate code
  3. The Database Code Generation dialog box for C#:
  4. Figure 1.18 - Database Code Generation dialog
  5. Click OK to generate the C# persistent code to the Output Path.

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com