MyGenertion is a good tool for generating ORM code. The following are some notes during my learning process (summarize the dOOdads C # Quick Reference documentation, you can also post the problems you encounter during use and debugging and Their Solutions) and share them with you.
This article mainly explains how to use the dOOdad template of MyGeneration (My MyGeneration version is 1.5.1) to generate code and add the code to the project.
1. Preparation: Database Design (MS SQL Server ):
1 ).Set an identity column for each table as the primary key. You can also use GUID or multiple columns as the primary key.
2 ).Add a column named "RowVersion" to each table, and set the data type to timestamp ). DOOdad will use this column to process concurrency.
3 ).DOOdad can only process a single table, but cannot process multi-table joint queries. To process multiple tables, you can create a view and try to replace multiple tables with operations.
4 ).Use as few null as possible when designing databases and applications.
2. Install MyGeneration and add the dOOdad project to the solution:
1 ).Download and install from http://www.mygenerationsoftware.com.
2 ).After the installation is complete, the dOOdad project is located in .. \ MyGeneration \ ubuntures \ dOOdad \ CSharp \ MyGeneration. in the dOOdad \ directory, there are two versions: 2003 and 2005. Select one as needed and add it to the solution. In addition, the directory contains a readme.txt file, which also describes the usage steps and instructions of dOOdas.
Of course, we can also directly open the project, compile and generate the dll file, and then add the Assembly reference to the application. However, the project code still has some problems, it may be discovered during debugging. it is inconvenient to modify the code and add references. Therefore, this is not recommended for individuals.
In the DbAdapters folder, by default, all file generation operation attributes are "NONE", that is, they are not compiled into the project. Select the two * Entity corresponding to the application database. cs and * DynamicQuery. cs files, and change their properties-build operation to "compile", so that they can be compiled into the generated Assembly.
Files of databases supported by MyGeneration:
SQL Server (SqlClientDynamicQuery. cs, SqlClientEntity. cs) System. Data. SqlClient
Access (OleDbDynamicQuery. cs, OleDbEntity. cs) System. Data. OleDb
Oracle (OracleClientDynamicQuery. cs, OracleClientEntity. cs) System. Data. OracleClient
PostgreSql (PostgreSqlDynamicQuery. cs, PostgreSqlEntity. cs) Npgsql
Firebird (FirebirdSqlDynamicQuery. cs, FirebirdSqlEntity. cs) FirebirdSql. Data. Firebird
VistaDB (VistaDBDynamicQuery. cs, VistaDBEntity. cs) VistaDB
SQLite (SQLiteDynamicQuery. cs, SQLiteEntity. cs) finsar. SQLite
MySQL (MySQL4DynamicQuery. cs, MySQL4Entity. cs) MySql. Data. MySqlClient
3 ).Now you can compile it.
3.Other projects that need to access the Assembly:
1 ).Add reference for project -- MyGeneration. dOOdad
2 ).Add two folders to the project:
DAL (data access layer): used to store Abstract classes created by MyGeneration (The following describes how to use MyGeneration to generate these classes ).
BLL (business logic layer): used to store specific classes of the Abstract class above, which can be automatically generated by MyGeneration.
4. Use MyGeneration:
4.1Generate a CRUD stored procedure:
1)Run MyGeneration, Edit -- DefaultSetting to set the database and programming language.
2)Click the toolbar-Template Browser and select the Template of the stored procedure to be created.
Available Templates include:
Microsoft SQL-Microsoft SQL Server. dOOdad Stored Procedures
Microsoft Access-Microsoft Access. Access Stored Procedures
Oracle-Oracle. Oracle Stored Procedures
PostgreSQL-PostgreSQL. PostgreSQL Stored Procedures
Firebird-Firebird. StoredProcedures. Firebird Stored Procedures
3)Run the template, select the database and table (generally select all tables), and click OK to OutPut the stored procedure in the OutPut.
4)Copy the generated stored procedure to the query analyzer and write it to the database.
4.2Generate a DAL Abstract class for the tables in the database:
Same as above, select Template Browser -- dOOdad -- C # (others are similar) -- dOOdad Business Entity -- execution Template -- select the DAL folder created above, set the namespace and database of the class to be generated, select all tables, and generate Abstract class.
4.3Generate a specific class for the view in the database:
Select the dOOdad Business View template and save the generated class in The BLL folder.
4.4Generate a specific class (you can also write it yourself ):
Select the dOOdad Concrete Class template.
5. Use the resources generated above in Visual Studio:
1)Configure the connection string in the configuration file web. config or app. config. In the dOOdad project, the default key is "dbConnection". You can also modify the value of the _ defaultConfig field in the BusinessEntity class to modify the default value of the key.
In addition, a problem with the dOOdad project is that the connection string is not initialized (it may be a template bug). The Code is as follows:
Internal string _ raw = "";
Virtual public string ConnectionString
...{
Get
...{
Return _ raw;
}
Set
...{
_ Raw = value;
}
}
Although you can manually assign a value to the ConnectionString attribute in the application, in this case, each business entity must assign a value to the ConnectionString after the new feature, which is cumbersome and difficult to maintain; A better way is to initialize _ raw in the BusinessEntity Constructor (write your own code ). I use VS2005. I can use VS2005 to automatically generate the Settings class for reading the configuration file and configuration file, and then initialize the connection string, for example, internal string _ raw = ProjectName. properties. settings. default. connectionString;
2)You can add your own business logic to the specific Class (Concrete Class) generated above to process custom stored procedures and SQL statements.