NHibernate学习的第一个星期
通过一个星期的学习下来,我越来越怀疑我是不是真的不行的了,搞了一个星期了,虽然只是利用一些空余时间 ,但是哪个该死的测试代码还是编译不过,我的代码是
XML文件
到这里应该说是没有什么问题 ,编译是通过了,用Nunit 测试还是出错,提示错误为:
这是怎么回事,天啊, 我的头要炸了,神啊救救我吧
using System;
namespace pb.testdal.Db
{
public class users
{
private System.String _Password;
public System.String Password
{
get { return _Password; }
set { _Password = value; }
}
private System.DateTime _LastLogon;
public System.DateTime LastLogon
{
get { return _LastLogon; }
set { _LastLogon = value; }
}
private System.String _Name;
public System.String Name
{
get { return _Name; }
set { _Name = value; }
}
private System.String _LogonID;
public System.String ID
{
get { return _LogonID; }
set { _LogonID = value; }
}
private System.String _EmailAddress;
public System.String EmailAddress
{
get { return _EmailAddress; }
set { _EmailAddress = value; }
}
}
}
namespace pb.testdal.Db
{
public class users
{
private System.String _Password;
public System.String Password
{
get { return _Password; }
set { _Password = value; }
}
private System.DateTime _LastLogon;
public System.DateTime LastLogon
{
get { return _LastLogon; }
set { _LastLogon = value; }
}
private System.String _Name;
public System.String Name
{
get { return _Name; }
set { _Name = value; }
}
private System.String _LogonID;
public System.String ID
{
get { return _LogonID; }
set { _LogonID = value; }
}
private System.String _EmailAddress;
public System.String EmailAddress
{
get { return _EmailAddress; }
set { _EmailAddress = value; }
}
}
}
XML文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="pb.testdal.Db.users, testdal1" table="users">
<id name="ID" column="LogonID" type="String(20)">
<generator class="assigned" />
</id>
<property name="Name" type="String" column="Name(40)"/>
<property name="Password" type="String" column="Password(20)"/>
<property name="EmailAddress" type="String" column="EmailAddress(20)"/>
<property name="LastLogon" type="DateTime" column="LastLogon" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="pb.testdal.Db.users, testdal1" table="users">
<id name="ID" column="LogonID" type="String(20)">
<generator class="assigned" />
</id>
<property name="Name" type="String" column="Name(40)"/>
<property name="Password" type="String" column="Password(20)"/>
<property name="EmailAddress" type="String" column="EmailAddress(20)"/>
<property name="LastLogon" type="DateTime" column="LastLogon" />
</class>
</hibernate-mapping>
这里有一点不能不提,就是主键一定要 以“id”为标识,我一个星期都不能搞定的错误就是由这个引起的,
我的测试代码
using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
namespace pb.testdal.Db
{
/// <summary>
/// testusers 的摘要说明。
/// </summary>
///
[TestFixture]
public class testusers
{
public testusers()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test] public void Testthisuser()
{
Configuration cfg=new Configuration();
cfg.AddAssembly("testdal1");
cfg.AddXmlFile("users.hbm.xml");
users newUser=new users();
ISessionFactory factory=cfg.BuildSessionFactory();
ISession session=factory.OpenSession();
ITransaction transaction=session.BeginTransaction();
newUser.ID="joe_cool";
newUser.Name="joseph Cool";
newUser.Password="abc123";
newUser.EmailAddress="joe@cool.com";
newUser.LastLogon=DateTime.Now;
session.Save(newUser);
transaction.Commit();
session.Close();
/*
session=factory.OpenSession();
users pingbo=(users)session.Load(typeof(users),"111111");
pingbo.LastLogon=DateTime.Now;
session.Flush();
IList recentUsers=session.CreateCriteria(typeof(users))
.Add(NHibernate.Expression.Expression.Gt("LastLogon",new DateTime(2004,09,29,16,0,0)))
.List();
foreach(users user in recentUsers)
{
Assert.IsTrue(user.LastLogon)(new DateTime(2004,09,29,16,0,0)));
}*/
session.Close();
}
}
}
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
namespace pb.testdal.Db
{
/// <summary>
/// testusers 的摘要说明。
/// </summary>
///
[TestFixture]
public class testusers
{
public testusers()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test] public void Testthisuser()
{
Configuration cfg=new Configuration();
cfg.AddAssembly("testdal1");
cfg.AddXmlFile("users.hbm.xml");
users newUser=new users();
ISessionFactory factory=cfg.BuildSessionFactory();
ISession session=factory.OpenSession();
ITransaction transaction=session.BeginTransaction();
newUser.ID="joe_cool";
newUser.Name="joseph Cool";
newUser.Password="abc123";
newUser.EmailAddress="joe@cool.com";
newUser.LastLogon=DateTime.Now;
session.Save(newUser);
transaction.Commit();
session.Close();
/*
session=factory.OpenSession();
users pingbo=(users)session.Load(typeof(users),"111111");
pingbo.LastLogon=DateTime.Now;
session.Flush();
IList recentUsers=session.CreateCriteria(typeof(users))
.Add(NHibernate.Expression.Expression.Gt("LastLogon",new DateTime(2004,09,29,16,0,0)))
.List();
foreach(users user in recentUsers)
{
Assert.IsTrue(user.LastLogon)(new DateTime(2004,09,29,16,0,0)));
}*/
session.Close();
}
}
}
pb.testdal.Db.testusers.Testthisuser : NHibernate.ADOException : could not synchronize database state with session
----> System.Data.SqlClient.SqlException : 第 1 行: '(' 附近有语法错误。
未能准备语句。
----> System.Data.SqlClient.SqlException : 第 1 行: '(' 附近有语法错误。
未能准备语句。