nhibernate save() 生成插入语句但没有实际记录插入到数据库中
我有以下代码。 在 SQL Server 探查器中,我可以看到正在生成插入语句,但没有插入实际记录。 我只是不明白为什么会发生这种事!
private ISessionFactory _sessionFactory;
private Configuration _configuration;
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof(Task).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
using (var s = _sessionFactory.OpenSession())
using (var t = s.BeginTransaction(IsolationLevel.RepeatableRead))
{
var taskToSave = new Task
{
Class = "test class",
IsActive = true,
Namespace = "test namespace"
};
s.FlushMode = FlushMode.Commit;
s.Save(taskToSave);
t.Commit();
}
我的映射文件是这样的:
<class name="Task" table="Task">
<id name="Id" column="Id" unsaved-value="0" type="Int32">
<generator class ="identity"></generator>
</id>
<property name="IsActive" column="IsActive" not-null="true" type="Boolean" />
<property name="Namespace" column="Namespace" length="255" not-null="true" type="String" />
<property name="Class" column="Class" length="255" not-null="true" type="String" />
</class>
谢谢! 顺便说一句,我正在使用 NHibernate-2.1.0.CR1。
I have the following code. In SQL Server profiler I can see the insert statement being generated however no actual record has been inserted. I just can't figure out why this is happening!
private ISessionFactory _sessionFactory;
private Configuration _configuration;
_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof(Task).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
using (var s = _sessionFactory.OpenSession())
using (var t = s.BeginTransaction(IsolationLevel.RepeatableRead))
{
var taskToSave = new Task
{
Class = "test class",
IsActive = true,
Namespace = "test namespace"
};
s.FlushMode = FlushMode.Commit;
s.Save(taskToSave);
t.Commit();
}
My mapping file is like this:
<class name="Task" table="Task">
<id name="Id" column="Id" unsaved-value="0" type="Int32">
<generator class ="identity"></generator>
</id>
<property name="IsActive" column="IsActive" not-null="true" type="Boolean" />
<property name="Namespace" column="Namespace" length="255" not-null="true" type="String" />
<property name="Class" column="Class" length="255" not-null="true" type="String" />
</class>
Thank you! BTW I am using NHibernate-2.1.0.CR1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为我在代码中的某个位置有
SchemaExport
而我没有意识到,当代码运行时 SchemaExport 创建了一个名为dbo_owner_Task
的新表,并将其插入到该表中而不是dbo.Task
。所以使用 SchemaExport 时要小心!
It was because I had
SchemaExport
somewhere in the code without me realizing it and when the code runs SchemaExport created a new table calleddbo_owner_Task
and inserted into that table instead ofdbo.Task
.So becareful when using SchemaExport!