Digress:
Although Microsoft launched EF4, it currently has very few virtual spaces that support EF4. Even if there is a supported space, it is very expensive. Therefore, using nh3.1 is a good choice, nh3.1 and EF4 support both LINQ and lambda expressions. If you are interested in EF4, refer to my previous blog post "Asp.net MVC 2.0 + unity 2.0 (IOC) + ef4.0 instance: rorowoblog open-source project framework code"
Required dll library files:
1. Download nhibernate-3.1.0.ga-bin.zip from the official website. The DLL file in the compressed package must be as follows:
NHibernate-3.1.0.GA-bin \ required_bins \Iesi. Collections. dll
NHibernate-3.1.0.GA-bin \ required_bins \Nhib.pdf. dll
NHibernate-3.1.0.GA-bin \ required_for_lazyloading \ linfu \Nhib.pdf. bytecode. linfu. dll
NHibernate-3.1.0.GA-bin \ required_for_lazyloading \ linfu \Linfu. dynamicproxy. dll
2. Download conform1.0.1.4_nh3.1ga.zip and use the DLL file in the compressed package as follows:
Conform1.0.1.4 _ nh3.1ga \Conform. dll
Create object class:
Using system. Collections. Generic;
Using system. text;
Using system;
Namespace dipipi. Infrastructure. hibernate. entity {
Public class blogtag {
Public blogtag (){}
Public Virtual int tag_id {Get; set ;}
Public Virtual string tagname {Get; set ;}
Public Virtual string articleids {Get; set ;}
}
}
Add, delete, and modify operation code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;Using nhib.pdf. dialect;
Using nhib.pdf. cfg;
Using nhib.pdf. cfg. loquacious;
Using nhib.pdf. cfg. mappingschema;
Using nhib.pdf. driver;
Using nhib.pdf. tool. hbm2ddl;
Using nhib.pdf. bytecode. linfu;
Using conform;
Using conform. NH;
Using dipipi. Infrastructure. hibernate. entity;
Namespace dipipi. Infrastructure. hibernate
{
Public static class dbhelper
{
Private const string _ connectionstring =
@ "Data Source = localhost; initial catalog = dipipidb; Integrated Security = true; pooling = false ";
Public static configuration getconfiguration ()
{
VaR configure = new configuration ();
Configure. sessionfactoryname ("dipipidb ");
// The proxy extension method is used to configure the bytecode provider for Nhibernate delayed loading.
Configure. Proxy (P => P. proxyfactoryfactory <proxyfactoryfactory> ());
Configure. databaseintegration (DB =>
{
DB. dialect <mssql2005dialect> ();
DB. Driver <sqlclientdriver> ();
DB. connectionstring = _ connectionstring;
});
Return configure;
}
Public static hbmmapping getmapping ()
{
VaR ORM = new objectrelationalmapper ();
Orm. tableperclass <blogtag> ();
VaR mapper = new mapper (ORM );
Return Mapper. compilemappingfor (new [] {typeof (blogtag )});
}
Public static void test ()
{
// Configure nhib.pdf
VaR conf = getconfiguration ();
// Add hbmmapping to configuration
Conf. adddeserializedmapping (getmapping (), "blogtag ");
// Configure the database architecture metadata
Schemametadataupdater. quotetableandcolumns (CONF );
// Create a database architecture
New schemaexport (CONF). Create (false, true );
// Create sessionfactory
VaR factory = Conf. buildsessionfactory ();
// Enable session for persistent data
Using (var s = factory. opensession ())
{
Using (VAR Tx = S. begintransaction ())
{
VaR blogtag = new blogtag {tagname = "My tests "};
S. Save (blogtag );
TX. Commit ();
}
}
// Query and sort
Using (var s = factory. opensession ())
{
VaR query = S. queryover <blogtag> ()
. Where (P => P. tagname = "My tests ")
. Orderby (P => P. tag_id). ASC
. List ();
}
// Open the session to delete data
Using (var s = factory. opensession ())
{
Using (VAR Tx = S. begintransaction ())
{
S. createquery ("delete from blogtag"). executeupdate ();
TX. Commit ();
}
}
// Delete the database architecture
New schemaexport (CONF). Drop (false, true );
}
}
}
It is also worth mentioning that new schemaexport (CONF ). create (false, true); here is the database created after the entity code is first written.