Nhibernate配置和访问数据问题
今天開始用Nhibernate做為自己的ORM,但是做的過程中確實(shí)遇到了好多問題,現(xiàn)在將問題收集起來以防日后出現(xiàn)相同的問題,
總結(jié)下:
這就是我的整個(gè)項(xiàng)目,現(xiàn)在配置下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>
?
現(xiàn)在新建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>
??????? /// 個(gè)人簡(jiǎn)介
??????? /// </summary>
??????? public string BriefIntroduction { get; set; }
??????? /// <summary>
??????? /// 創(chuàng)建時(shí)間
??????? /// </summary>
??????? public DateTime? CreateDt { get; set; }
??? }
}
現(xiàn)在映射這個(gè)User.cs類,新建User.hbm.xml文件并且進(jìn)行配置,如下:
<?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>
注意設(shè)置User.hbm.xml文件屬性,XML文件的默認(rèn)生成操作為“內(nèi)容”,這里需要修改為“嵌入的資源”,
然后測(cè)試:
? public IList<User> Get()
??????? {
??????????? try
??????????? {
??????????????? return _session.CreateQuery("from Subject.Model.User").List<User>();
??????????? }
??????????? catch (Exception e)
??????????? {
??????????????? throw;
??????????? }
??????? }
發(fā)現(xiàn)數(shù)據(jù)為空,如圖:
不可能啊?因?yàn)閿?shù)據(jù)庫里有數(shù)據(jù)啊,如圖:
這是怎么回事,找了好久,終于被我找到問題的所在,如圖:
原來要加這個(gè),但是運(yùn)行之后又出現(xiàn)了問題,唉,寫個(gè)NHibernate出現(xiàn)這么多問題,自己跟自己說:淡定!
你猜是什么問題?原來Sqlserver2008系統(tǒng)表里有User表,你再建這個(gè)表就會(huì)有歧義,當(dāng)然就會(huì)出現(xiàn)問題了,
所以我把User表改成Users表就OK了!
終于成功了,不容易啊!
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhangwei595806165/p/3499562.html
總結(jié)
以上是生活随笔為你收集整理的Nhibernate配置和访问数据问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridView用法详解
- 下一篇: js 网页输出文本