nhibernate save() 生成插入语句但没有实际记录插入到数据库中

发布于 2024-07-26 20:02:10 字数 1358 浏览 2 评论 0原文

我有以下代码。 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2024-08-02 20:02:10

这是因为我在代码中的某个位置有 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 called dbo_owner_Task and inserted into that table instead of dbo.Task.

So becareful when using SchemaExport!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文