ORM design tool: Mindscape nhib1_designer. Install the Mindscape. nhibernatemodeldesigner. vsix file.
It is a plug-in of Visual Studio 2010, as shown in the running time.
Model frist and database first development modes are supported, as shown in
Update model from database, update model definition from database schema
Update database from model is used to update the database as an object definition.
The designer generates a component layout file for the designer to work. a cs file maps the object attributes to the database field. The agent content is as follows:
Public partial class agent
{
Public Virtual string name {Get; set ;}
Public Virtual string position {Get; set ;}
Public Virtual string description {Get; set ;}
Static partial void customizemappingdocument (system. xml. LINQ. xdocument mappingdocument );
Internal static system. xml. LINQ. xdocument mappingxml
{
Get
{
VaR mappingdocument = system. xml. LINQ. xdocument. parse (@ "<? XML version = '1. 0' encoding = 'utf-8'?>
<Hibernate-mapping xmlns = ''n': nhibernate-mapping-2.2'
Assembly = '"+ typeof (agent). Assembly. getname (). Name + @"'
Namespace = 'businesslogic'
>
<Class name = 'agent'
Table = ''agent''
>
<ID name = 'name'
Column = ''name''
>
<Generator class = 'identified'>
</Generator>
</ID>
<Property name = 'position'
Column = ''position''
/>
<Property name = 'description'
Column = ''description''
/>
</Class>
</Hibernate-mapping> ");
Customizemappingdocument (mappingdocument );
Return mappingdocument;
}
}
}
The value of mappingxml is the ing file that needs to be configured when we develop Nhibernate, which is maintained by the designer.
MySQL 5.1 is used as the database.
Create a MySQL DATA table
The SQL script is as follows:
Drop table if exists 'ctu '. 'agent ';
Create Table 'ctu '. 'agent '(
'Name' varchar (40) not null,
'Position' varchar (45) not null,
'Description' varchar (45) not null,
Primary Key ('name ')
) Engine = InnoDB default charset = Latin1;
First, check the data items in this table. There are three data items, with Jack, Tony, and Charles as the primary keys.
Start Visual Studio 2010, Open Server Explorer, and create a new MySQL connection
Remember to check allow saving password here. Otherwise, when you drag the table to the designer, an exception that cannot be connected will occur.
Drag the table CTU to the nhib.pdf designer to create the model.
Right-click the designer to bring up the get started menu item.
The main content of the configuration file has been written. Add the app. config or web. config file and copy the configuration content to the file.
<Configsections>
<Section name = "hibernate-configuration" type = "nhib.pdf. cfg. configurationsectionhandler, nhib.pdf"/>
</Configsections>
<Hibernate-configuration xmlns = "urn: nhibernate-configuration-2.2">
<Session-factory>
<Property name = "dialect"> nhib.pdf. dialect. mysql5dialect </property>
<Property name = "connection. connection_string"> User ID = root; Password = 123; host = jamesli; database = CTU; persist Security info = true; </property>
<Property name = 'proxyfactory. factory_class '> nhib.pdf. bytecode. Castle. proxyfactoryfactory, nhib.pdf. bytecode. Castle </property>
</Session-factory>
</Hibernate-configuration>
In the project startup directory, add the twoProgramSet: nhib.pdf. bytecode. Castle. dll and Castle. Core. dll
Copy the nhibernatehelper file in get started to the project.
Public static class nhibernatehelper
{
Private Static isessionfactory _ sessionfactory;
Internal static isessionfactory sessionfactory
{
Get
{
If (_ sessionfactory = NULL)
{
VaR configuration = configurationhelper. createconfiguration ();
_ Sessionfactory = configuration. buildsessionfactory ();
}
Return _ sessionfactory;
}
}
Internal static isession opensession ()
{
Return sessionfactory. opensession ();
}
}
Design the following test Code
[Testmethod]
Public void testfetch ()
{
Using (isession session = nhibernatehelper. opensession ())
Using (itransaction Tx = session. begintransaction ())
{
String name = "Tony ";
Agent Tony = session. Get <agent> (name );
String fullname = Tony. description;
}
}
Start the debugger and enter the method. The effect is shown in.
This indicates that nhib.pdf has successfully obtained data records from the MySQL database.
Starting from. NET 1.1, nhib.pdf is a highly recommended ORM development tool. Because there is no satisfactory ORM design tool, it is very troublesome to maintain the ing between database fields and object attributes, and it is prone to errors. One of the selling points of commercial ORM tools is to simplify the design of ing files. Whether it is using XML files or CS files to maintain mappings, as long as the maintenance relationship ing is simple, it is a great improvement.ArticleThe database and entity ing content mentioned in nhib.pdf should be simple and lightweight to develop ERP/MIS.
After writing this document, check the nhib.pdf document. There is a software for nhib.pdf. tool. hbm2net, which is explained as follows:
Nhib.pdf. tool. hbm2net is an additional software for nhib.pdf, which generates data from the HBM. xml ing file.Source codePossible.
In the nhib.pdf. Tasks directory, there is a tool named hbm2nettask that you can use to automatically compile programs (using Nant ).
We recommend another tool, Nhibernate profiler.
Add reference to hibernatingrhinos. nhib.pdf. profiler. appender. dll assembly in the application and add code at startup
[Testinitialize]
Public void initializeprofiler ()
{
Hibernatingrhinos. nhib.pdf. profiler. appender. nhibernateprofiler. initialize ();
}
Hibernatingrhinos. nhib.pdf. profiler. appender. nhibernateprofiler. initialize ();
Insert the tracing program, start the previous test method, and switch to the nhib?profiler interface to see the SQL statement sent to the server.
Of course, you can also use SQL Server Profiler to track problematic SQL statements. For example,
salesorderentity salesorder = session. get (5023);
This is a primary key used to find data records. Generally, no problem occurs. Write two SQL statements to compare them, see the following two SQL statements
select * From salesorder where customerid is null
select * From salesorder where customerid = ''
sentence, from the SQL perspective, the previous sentence is to find a purchase order with the customer number being null, And the next sentence is to find a purchase order with the customer number being blank
corresponds to customerid, generally, the string type is used for ing. If a statement is available, convert customerid to string. empty, while another piece of code
has not initially attempted customerid, which means that customerid is null. The two ORM statements are sent to the server, which is two different where conditions, that is, customerid = ''and customerid is null, that is, the sent ORM statement is inconsistent with the actual SQL query results.
During ORM development, if the semantics of An ORM statement is different from the SQL statement we imagine, you can use nhib1_profiler or SQL Server Profiler to track problematic SQL statements, in this way, you can modify the ORM writing method to eliminate errors.