NHibernate
NHibernate是一个面向.NET框架的对象关系映射解决方案。主要用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。 NHibernate,顾名思义,如同NUnit,NAnt一样,是基于.Net的Hibernate实现。
例子這裡有程式碼片段,是要使用NHibernate將物件加入資料庫,和展示如何取得、修改、更新資料庫中的物件。 //Add a Customer to the datastore
//'sessionFactory' is a thread-safe object built once per application lifetime (can take seconds to build)
//based on configuration files which control how database tables are mapped to C# objects
//(e.g. which property maps to which column in a database table)
//
//'session' is not thread safe and fast to obtain and can be thought of as a connection to the database
using (var session = sessionFactory.OpenSession())
{
//transaction represents a db transaction
using (ITransaction transaction = session.BeginTransaction())
{
//The line below adds the customer to NHibernate's list of objects to insert to the database
//but it doesn't execute SQL insert command at this stage*.
//*if the Id field is generated by the database (e.g. an auto-incremented number)
//then NHibernate will execute SQL INSERT when .Save is called
session.Save(new Customer { Id = Guid.NewGuid(), FirstName = "Boss", Age = 50 });
//The call below will execute the SQL INSERT and commit the transaction
transaction.Commit();
}
}
//Retrieve the Customer from the database, modify the record and update the database
using (var session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
//session's Query returns IQueryable<Customer>.
//Only when .FirstOrDefault is called will NHibernate execute the SQL query
Customer customer = session.Query<Customer>().Where(c => c.Token == token ).FirstOrDefault();
//Now the customer is 'part of' the 'session' object and NHibernate keeps track of changes
//made to it
if( customer != null )
{
//Changing a property of an object does NOT cause SQL to be executed
customer.TokenVerified = true;
//Committing the transaction results in an SQL UPDATE statement
//NHibernate kept track of the fact that 'customer' has been changed since loading
transaction.Commit();
}
}
}
NHibernate's configuration may affect when NHibernate executes SQL statements. |
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve