Nhibernate配置和访问数据问题
今天開始用Nhibernate做為自己的ORM,但是做的過程中確實遇到了好多問題,現在將問題收集起來以防日后出現相同的問題,
總結下:
這就是我的整個項目,現在配置下hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
<hibernate-configuration? xmlns="urn:nhibernate-configuration-2.2" >
?? ?<session-factory name="NHibernate.Test">
?? ??? ?<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
?? ??? ?<property name="connection.connection_string">
????? server=127.0.0.1;database=Subject;uid=sa;pwd=zhangwei
??? </property>
?? ??? ?<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
? </session-factory>
</hibernate-configuration>
?
現在新建User.cs類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Subject.Model
{
??? /// <summary>
??? /// 用戶表
??? /// </summary>
??? public class User
??? {
??????? /// <summary>
??????? /// 用戶id
??????? /// </summary>
??????? public string Id { get; set; }
??????? /// <summary>
??????? /// 名稱
??????? /// </summary>
??????? public string Name { get; set; }
??????? /// <summary>
??????? /// 密碼
??????? /// </summary>
??????? public string Password { get; set; }
??????? /// <summary>
??????? /// 性別
??????? /// </summary>
??????? public string Sex { get; set; }
??????? /// <summary>
??????? /// 個人簡介
??????? /// </summary>
??????? public string BriefIntroduction { get; set; }
??????? /// <summary>
??????? /// 創建時間
??????? /// </summary>
??????? public DateTime? CreateDt { get; set; }
??? }
}
現在映射這個User.cs類,新建User.hbm.xml文件并且進行配置,如下:
<?xml version="1.0" encoding="utf-8" ?>
? <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
??? namespace="Subject.Model" assembly="Subject.Model">
? <class name="Subject.Model.User" table="User" lazy="false">
??? <id name="Id">
????? <column name="Id" sql-type="varchar(40)" not-null="true"/>
????? <generator class="uuid.hex" />
??? </id>
??? <property name="Name">
????? <column name="Name" sql-type="varchar(20)" not-null="false" />
??? </property>
??? <property name="Password">
????? <column name="Password" sql-type="varchar(20)" not-null="false" />
??? </property>
??? <property name="Sex">
????? <column name="Sex" sql-type="varchar(2)" not-null="false" />
??? </property>
??? <property name="BriefIntroduction">
????? <column name="BriefIntroduction" sql-type="varchar(500)" not-null="false" />
??? </property>
??? <property name="CreateDt">
????? <column name="CreateDt" sql-type="datetime" not-null="false" />
??? </property>
? </class>
</hibernate-mapping>
注意設置User.hbm.xml文件屬性,XML文件的默認生成操作為“內容”,這里需要修改為“嵌入的資源”,
然后測試:
? public IList<User> Get()
??????? {
??????????? try
??????????? {
??????????????? return _session.CreateQuery("from Subject.Model.User").List<User>();
??????????? }
??????????? catch (Exception e)
??????????? {
??????????????? throw;
??????????? }
??????? }
發現數據為空,如圖:
不可能啊?因為數據庫里有數據啊,如圖:
這是怎么回事,找了好久,終于被我找到問題的所在,如圖:
原來要加這個,但是運行之后又出現了問題,唉,寫個NHibernate出現這么多問題,自己跟自己說:淡定!
你猜是什么問題?原來Sqlserver2008系統表里有User表,你再建這個表就會有歧義,當然就會出現問題了,
所以我把User表改成Users表就OK了!
終于成功了,不容易啊!
?
?
轉載于:https://www.cnblogs.com/zhangwei595806165/p/3499562.html
總結
以上是生活随笔為你收集整理的Nhibernate配置和访问数据问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridView用法详解
- 下一篇: js 网页输出文本