我开始学习NHibernate。我安装了最新版本并尝试运行一个非常简单的应用程序。有一个班级User
:
public class User
{
public virtual Guid Id { get; set; }
public virtual String Name { get; set; }
public virtual int Age { get; set; }
public override string ToString()
{
return string.Format("User{name='{0}', age={1}}", Name, Age);
}
}
它的映射是:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="cs_nhibernate_test1"
namespace="cs_nhibernate_test1">
<class name="User">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Age" />
</class>
</hibernate-mapping>
然后,我的测试程序是这样的:
class Program
{
static void Main(string[] args)
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof (User).Assembly);
new SchemaExport(cfg).Execute(false, true, true);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
User user = new User { Name = "John Smith", Age = 25 };
session.Save(user);
transaction.Commit();
}
}
}
}
hibernate.cfg.xml是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="connection.connection_string">Data Source=FirstSample.sdf</property>
<property name="show_sql">true</property>
</session-factory>
该项目是.NET 4.0项目,所有必需的文件似乎都在输出文件夹中。
我得到的错误是:
NHibernate:INSERT INTO用户(姓名,年龄,身份)VALUES(@ p0,@ p1, @ p2); @ p0 ='John Smith'[类型:字符串(0)],@ p1 = 25 [类型:Int32 (0)],@ p2 = 4b0d4bbf-7d3c-4bff-908d-ecddfdd8a5cd [类型:指南(0)]
未处理的异常:System.Data.SqlServerCe.SqlCeException:在那里 解析查询时出错。 [令牌行号= 1,令牌行 offset = 13,Toke n in error = User]
关于修复它的任何想法?