关于Nhibernate配置问题
我是刚开始用Nhibernate,老是出现问题:映射文件user.hbm.xml找不到架构信息 ,是和我的Nhibernate的版本有关吗?
有没有朋友曾遇到过类似的问题,帮小弟一把,多谢了!
具体代码如下:
1.Users类
namespace guestbook.data
{
public class Users
{
public Users()
{ }
private System.Int32 _flag;
public System.Int32 flag
{
get { return _flag; }
set { _flag = value; }
}
private System.String _email;
public System.String email
{
get { return _email; }
set { _email = value; }
}
private System.Int32 _id;
public System.Int32 id
{
get { return _id; }
set { _id = value; }
}
private System.DateTime _regtime;
public System.DateTime regtime
{
get { return _regtime; }
set { _regtime = value; }
}
private System.String _Name;
public System.String Name
{
get { return _Name; }
set { _Name = value; }
}
private System.String _password;
public System.String password
{
get { return _password; }
set { _password = value; }
}
}
}
2.User.hbm.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-1.0">
<class name="guestbook.data.users, guestbook.data" table="G_users">
<id name="id" column="id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" type="String(20)" column="Name" />
<property name="email" type="String(40)" column="email" />
<property name="password" type="String(20)" column="password" />
<property name="flag" type="Int32" column="flag" />
<property name="regtime" type="DateTime" column="regtime" />
</class>
</hibernate-mapping>
3.App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
<add key="hibernate.connection.connection_string" value="Server=localhost;initial catalog=NhibernateTest;User ID=sa;Password=sa;Min Pool Size=2" />
</nhibernate>
</configuration>
4.测试代码
namespace guestbook.test
{
/**/
/// <summary>
/// DataLayerUsersTest 的摘要说明。
/// </summary>
[TestFixture]
public class DataLayerUsersTest
{
public DataLayerUsersTest()
{
}
[Test]
public void testAdd()
{
Configuration cfg = new Configuration();
cfg.AddXmlFile("User.hbm.xml");
ISessionFactory f = cfg.BuildSessionFactory();
ISession s = f.OpenSession();
ITransaction t = s.BeginTransaction();
Users newUser = new Users();
newUser.Name = "易做图snake";
newUser.password = "24976904";
newUser.email = "易做图snakes@gmail.com";
newUser.regtime = DateTime.Now;
s.Save(newUser);
t.Commit();
s.Close();
}
}
} --------------------编程问答-------------------- 注意大小写和命名空间, 配置文件和实体最好用mygeneration 生成,不要手写,很容易出错的 --------------------编程问答-------------------- 看了
很是 麻烦 --------------------编程问答-------------------- 不要手写,很容易出错 --------------------编程问答-------------------- 不是版本的问题,是设置的问题!
补充:.NET技术 , .NET技术前瞻