fbpx
维基百科

NHibernate

NHibernate是一个面向.NET框架对象关系映射解决方案。主要用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。

NHibernate
当前版本
  • 5.4.6 (2023年8月27日;穩定版本)[1]
源代码库
  • github.com/nhibernate/nhibernate-core
编程语言C Sharp
操作系统跨平台
许可协议GNU宽通用公共许可证
网站http://nhibernate.info

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.

  1. ^ Release 5.4.6. 2023年8月27日 [2023年9月18日]. 

nhibernate, 此條目可参照外語維基百科相應條目来扩充, 2012年6月12日, 若您熟悉来源语言和主题, 请协助参考外语维基百科扩充条目, 请勿直接提交机械翻译, 也不要翻译不可靠, 低品质内容, 依版权协议, 译文需在编辑摘要注明来源, 或于讨论页顶部标记, href, template, translated, page, html, title, template, translated, page, translated, page, 标签, 此條目没有列出任何参考或来源, 2010年12月28日. 此條目可参照外語維基百科相應條目来扩充 2012年6月12日 若您熟悉来源语言和主题 请协助参考外语维基百科扩充条目 请勿直接提交机械翻译 也不要翻译不可靠 低品质内容 依版权协议 译文需在编辑摘要注明来源 或于讨论页顶部标记 a href Template Translated page html title Template Translated page Translated page a 标签 此條目没有列出任何参考或来源 2010年12月28日 維基百科所有的內容都應該可供查證 请协助補充可靠来源以改善这篇条目 无法查证的內容可能會因為異議提出而被移除 NHibernate是一个面向 NET框架的对象关系映射解决方案 主要用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去 NHibernate当前版本5 4 6 2023年8月27日 穩定版本 1 源代码库github wbr com wbr nhibernate wbr nhibernate core编程语言C Sharp操作系统跨平台许可协议GNU宽通用公共许可证网站http nhibernate infoNHibernate 顾名思义 如同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 lt Customer gt Only when FirstOrDefault is called will NHibernate execute the SQL query Customer customer session Query lt Customer gt Where c gt 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 Release 5 4 6 2023年8月27日 2023年9月18日 取自 https zh wikipedia org w index php title NHibernate amp oldid 60464377, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。