这是我的cfg文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">Data Source=nhibernateTest.db;Version=3</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
这是我的hbm文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHExample"
namespace="NHExample.Domain">
<class name="Product" table="Products">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Price" />
</class>
</hibernate-mapping>
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
namespace NHExample.Domain
{
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual int Price { get; set; }
}
class Program
{
// Initialize NHibernate
static Configuration cfg = new Configuration();
static void Main(string[] args)
{
cfg.Configure();
cfg.AddAssembly(typeof(Product).Assembly);
ISessionFactory session = cfg.BuildSessionFactory();
ISession sess = session.OpenSession();
sess.BeginTransaction();
Product product = new Product
{
Name = "Some C# Book",
Category = "Books",
Price = 500,
};
sess.Save(product);
sess.Transaction.Commit();
IQuery q = sess.CreateQuery("FROM Products");
var list = q.List<Product>();
// List all the entries' names
list.ToList().ForEach(p => Console.WriteLine(p.Name));
}
}
}
我收到错误:
at:sess.Transaction.Commit();
任何想法可能出错?我听说它可以与不存在的数据库连接 - 但数据库是自动创建的,虽然它的重量为0字节......我还需要做些什么才能使其正确?