The nhibernateorm tool uses llbl Gen 3.1 and has a built-in Nhibernate designer. From the generatedCodeIt moves the ing between database fields and object attributes to the. NET code based on the functions provided by fluentnhib.pdf. The ORM ing is maintained by the ORM designer.
First, let's look at the database content. The agent table definition is as follows:
The result of executing the query command is as follows, which is used to verify the correctness of the ORM statement written later.
Start llbl Gen, create a nhiberate-based project, and connect to the database
Note: SQL server can use (local) and. As the current machine name, But MySQL does not know it and uses localhost
Then, in the category explorer form, click reverse-engineer tables to entity definitions to obtain the object definition.
Shows the final project structure.
F7: generate the project code, enter the top-level namespace paradox. businesslogic, and generate two projects: Model and persistence.
The project solution view is as follows:
A model is the definition of an object. Persistence includes the ing definition between an object and a database. It references nhib.pdf and fluentnhib.pdf.
Fluentnhibernate advocates that the solution to processing ing is to use code files. Please refer to fluentnhibernate'sArticleLearn more.
Write a method to test database reading
Using (isession session = sessionmanager. opensession ())
{
Agent Jack = session. Get <agent> ("Jack ");
String description = Jack. description;
}
RunProgram.
To analyze the persistence project of the code generated by the Nhibernate designer of llbl Gen, there is a sessionmanager class.
Public static partial class sessionmanager
{
Static sessionmanager ()
{
_ Sessionfactory = fluently. Configure ()
. Database (mysqlconfiguration. Standard
. Connectionstring (C => C. fromconnectionstringwithkey ("connectionstring. MySQL (mysqldirect )"))
. Proxyfactoryfactory ("nhib.pdf. bytecode. Castle. proxyfactoryfactory, nhib.pdf. bytecode. Castle "))
. Mappings (M => M. fluentmappings. addfromassembly (typeof (sessionmanager). Assembly ))
. Buildsessionfactory ();
}
Public static isession opensession ()
{
Return _ sessionfactory. opensession ();
}
Public static isessionfactory sessionfactory
{< br> get {return _ sessionfactory ;}< BR >}< br> If fluentnhib.pdf is not required, you may need to write such a configuration file
nhib.pdf. dialect. mysql5dialect
User ID = root; Password = 123; host = jamesli; database = CTU; persist Security info = true;
nhib.pdf. bytecode. castle. proxyfactoryfactory, nhib.pdf. bytecode. castle
the configuration content has been moved to the C # code. The entity definition agent in the model project corresponds to an agentmap In the persistence project. Its Code definition is as follows
Public partial class agentmap: classmap
{< br> Public agentmap ()
{< br> table ("'agent'");
optimisticlock. none ();
lazyload ();
ID (x => X. Name)
. Access. camelcasefield (prefix. Underscore)
. Column ("name ")
. Generatedby. Assigned ();
Map (x => X. Description). Access. camelcasefield (prefix. Underscore );
Map (x => X. Position). Access. camelcasefield (prefix. Underscore );
Additionalmappinginfo ();
}
Partial void additionalmappinginfo ();
}
This is a knowledge point of the fluentnhib.pdf framework. For more information, see fluentnhib.pdf.
One of the selling points of llbl Gen 3. x series is that it is a designer that supports multiple ORM frameworks. For example, Entity Framework, nhib.pdf, and llbl Gen runtime are supported. So far, the official site has not added the nhib.pdf sample, and it is difficult to find a complete example program. After fierce competition among many ORM frameworks, companies that have adopted the Nhibernate framework may have created their own ing file generation tools, such as code Smith or custom tools. For companies that do not use nhib.pdf, it is risky to set up the product on the nhib.pdf designer and tools of llbl Gen 1.0 for the first time. For companies that have used other ORM frameworks, if you want to port them to the Nhibernate framework, you can try the llbl Gen 3. x designer.
Of course, the safest way is to follow Microsoft and honestly use its Entity Framework to avoid the pain of porting ORM APIs.