Transferred from: http://www.cnblogs.com/leitwolf/archive/2007/07/27/833255.html
http://blog.csdn.net/happyhippy/article/details/1105875
Http://baike.baidu.com/link?url=-CbEabTswlJfVGssgYmHfz3kd6ui2RfU1kUe02e5V_54LvXqzu5sxrgUOV7A0v9qol6JQPM9GlNzQJ_8GTjOCa
mygeneration Study Notes (1): Using mygeneration to generate stored procedures and data access layer codeCategory: ORM2006-08-22 16:04 6666 People read Comments (4) favorite reports Store Microsoft Database Postgresqlsql serverassembly
Mygenertion is a good code generation ORM Code generation tool, here are some of my learning process notes (summed up the Doodads C # Quick reference documentation, as well as their own in the use and debugging process encountered problems and their own solutions), Post it and share it with everyone.
This article mainly explains how to generate code using the Doodad template (with its own template) for Mygeneration (the version number of the mygeneration that I used), 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 a GUID or multiple columns as the primary key.
2). add a column named "RowVersion" for each table and set the data type to timestamp (timestamp). Doodad will use this column to handle concurrency.
3). Doodad can only handle single tables and cannot handle multi-table union queries. To work with more than one table, you can create a view that attempts to replace multiple table problems with an action.
4). use NULL as little as possible when designing databases and applications.
2. Install mygeneration, add Doodad project to Solution:
1). download and install from http://www.mygenerationsoftware.com.
2).After the installation is complete, the Doodad project is located at: /mygeneration/architectures/doodad/csharp/mygeneration.doodad/directory, there are two versions, 2003 and 2005, depending on your needs, select one to add to the solution. In addition, there is a Readme.txt file under the directory, and some doodas steps and instructions are also used.
Of course, we can also open the project directly, compile the build DLL file, and then add the assembly reference in the application, but the code of the project still has some problems, it may be found in debugging, it is inconvenient to modify the code to add references, so it is not recommended to do so personally.
Under the Dbadapters folder, the default all file generation action properties are none, which is not compiled into the project. Select the two *entity.cs and *dynamicquery.cs two files corresponding to the application's database, and change their attribute-build action to "compile" so that it can be compiled into the generated assembly.
Files supported by the Mygeneration database:
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) finisar.sqlite
MySQL (MySQL4DynamicQuery.cs, MySQL4Entity.cs) MySql.Data.MySqlClient
3).Now it's ready to compile.
3. Other items that require access to the assembly:
1). Add a reference to the project-project--mygeneration.doodad
2). add two folders to the project:
DAL (data access Layer): Used to hold the abstract class created by Mygeneration (below is how to use mygeneration to generate these classes).
BLL (business logic Layer): A concrete class that is used to inherit the abstract class above, which can be generated automatically by Mygeneration.
4.using mygeneration:
4.1To generate a crud stored procedure:
1)Run mygeneration,edit--defaultsetting, set up databases, programming languages, and more.
2)Click the toolbar-template Browser, select the template for the stored procedure you want to create
The available templates are:
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 the table (typically all tables are selected), and when you click OK, the stored procedure is exported in output
4)Copies the generated stored procedure to the Query Analyzer, and then executes the write to the stored procedure in the database.
4.2 generates the Dal abstract class for tables in the database:
Ditto, choose Template browser--doodad--c# (other 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, The abstract class is then generated.
4.3 Generate concrete classes for the views in the database:
Select the Doodad business View template and save the generated class in the BLL folder, as in the other.
4.4 Generate concrete classes (you can also write them yourself):
Select the Doodad concrete class template, as in the other ibid.
5. Use the resources generated above in Visual Studio:
1) Configure the connection string in configuration file Web. config or app. config. The default key in the Doodad project is "DbConnection", or you can modify the value of the _defaultconfig field in the BusinessEntity class to modify the key's default value.
In addition, the problem with the Doodad project is that the connection string is not initialized (possibly a template bug) and the code is as follows:
Internal string _raw = "";
Virtual public string ConnectionString
{
Get
{
return _raw;
}
Set
{
_raw = value;
}
}
Although it is possible to assign a property connectionstring to an attribute manually in the application, each business entity will be assigned a value for ConnectionString after the new is completed, which is cumbersome and difficult to maintain. A better approach is to initialize the _raw in the BusinessEntity constructor (write your own code). I'm using VS2005, you can use VS2005 to automatically generate read class settings for configuration files and configuration files, and then initialize the connection string, for example: internal string _raw = ProjectName.Properties.Settings.Default.ConnectionString;
2) You can add the application's own business logic to the specific class (concrete class) generated above to handle custom stored procedures and SQL statements.
- Next Mygeneration study Note (2): Use common operations provided by Doodad
Mygeneration code Generator