似乎插入已成功完成,但该项目实际上从未插入到表中。
如果我在插入后立即进行控制台转储,它会将项目显示为已插入,但是当我在表上显示数据时,它不会反映此更改。
如果我选择“显示表数据”,则不会反映更改,但我在服务器资源管理器中启动新的插入和查询,那么它会反映正确的更改。
GITHUB 项目:https ://github.com/Fabii23/NHibernate.git
SQL 输出:
INSERT INTO Products (Name, Category, Discontinued) VALUES (@p0, @p1
, @p2);@p0 = 'Barley and Oats' [Type: String (0)], @p1 = 'Grains' [Type: String
(0)], @p2 = 0 [Type: Int32 (0)]
NHibernate: select @@IDENTITY
try
{
//Try an insert
using (ISession session = NHibernateTest.NHibernateHelper.GetCurrentSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
int _bool = 1;
var product = new Product("Wonder Bread", "Bread", _bool);
session.Save(product);
transaction.Commit();
}
}
}
catch (Exception e)
{
Console.WriteLine("Error occurred :" + e.Message);
Console.WriteLine("Error occurred :" + e);
}
}
请注意,ID自动递增
表列:
整数ID | 字符串 名称 | 字符串 类别 | 停产|
sql输出:
NHibernate: INSERT INTO Products (Name, Category, Discontinued) VALUES (@p0, @p1
, @p2);@p0 = 'Wonder Bread' [Type: String (0)], @p1 = 'Bread' [Type: String (0)]
, @p2 = 1 [Type: Int32 (0)]
NHibernate: select @@IDENTITY
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="HibernateExample" namespace="HibernateExample.Domain" >
<class name="Product" table="Products">
<id name="Id" type="integer">
<generator class="identity"/>
</id>
<property name="Name" type="string"/>
<property name="Category" type="string"/>
<property name="Discontinued" />
</class>
</hibernate-mapping>
休眠规格:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class"> NHibernate.Driver.SqlServerCeDriver</property>
<property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
<property name="connection.connection_string">Data Source=FirstSample.sdf;</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>