编辑“︁
NHibernate
”︁(章节)
跳转到导航
跳转到搜索
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
== 例子 == 這裡有程式碼片段,是要使用NHibernate將物件加入資料庫,和展示如何取得、修改、更新資料庫中的物件。 <syntaxhighlight lang="csharp"> //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(); } } } </syntaxhighlight> NHibernate's configuration may affect when NHibernate executes SQL statements.
摘要:
请注意,所有对Local Chinese Wikipedia的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Project:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
大陆简体
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息