1

It seems as if the insert is completed successfully , but the item is never actually inserted into the table.

If I do a console dump immediately after the insert, it shows the item as being inserted, but when I do a show data on the table, it does not reflect this change.

If I select "Show Table Data" the changes aren't reflected, but I initiate a new insert and query within server explorer then it reflects the proper changes.

Project on GITHUB : https://github.com/Fabii23/NHibernate.git

SQL output:

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);
        }
}

Note that Id autoincrements

Table columns:

int Id | string Name | string Category | bit Discontinued |


Sql output :

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 specs:

  <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>
Fabii
  • 3,820
  • 14
  • 51
  • 92
  • Did the session close/flush? NHibernate will batch up data access until the session is flushed - I assume your `using` is supposed to dispose/flush the sesh - what ISession implementation are you using? – Charleh Jan 28 '13 at 15:54
  • @Charleh Yes, and the ISession implementation is NHibernate.ISession – Fabii Jan 28 '13 at 15:59
  • Well the code looks ok to me - it's definitely not a rollback then? – Charleh Jan 28 '13 at 16:03
  • @Charleh No I dont think so, If I do a console dump immediately after the insert, it shows the item as being inserted, but when I do a show data on the table, it does not reflect this change. – Fabii Jan 28 '13 at 16:13
  • 3
    At the risk of sounding like Captain Obvious, are you sure you're connecting to the right database? Is this running as part of a unit test? Is Setup/Teardown refreshing the database? It's happened to me quite a few times :) – Ragesh Jan 28 '13 at 16:22
  • @Ragesh, lol that's a reasonable question and yes I am connecting to the correct db. I only have the only have the one db anyways. – Fabii Jan 28 '13 at 16:31
  • This running on MSSQL? I'd consider profiling the server just to see what commands actually hit – Charleh Jan 28 '13 at 16:46
  • @Charleh, I posted the hibernate and db specs in the edit above. – Fabii Jan 28 '13 at 16:58

2 Answers2

2

Based on the code you posted on github, you should be aware that the database which will be updated is the one located in bin\debug directory (for debug mode), not the one at the root of your project source directory.

Be also aware that this database will be overwritten (and reinitialized) each time you recompile your application.


Does it change something if you change this line :

<property name="connection.connection_string">Data Source=FirstSample.sdf;</property>

to

<property name="connection.connection_string">Data Source=FirstSample.sdf;FLUSH INTERVAL=1</property>

( according to http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/cac9593a-4ee2-4f62-897f-96204af45a27/ )

see also resolving corruption in SQL Server Compact Edition database files

Community
  • 1
  • 1
jbl
  • 15,179
  • 3
  • 34
  • 101
  • I added the change above, Im still running into the same issue though. – Fabii Jan 29 '13 at 15:16
  • @Fabii thx for the answer. What I would try then - check if not a nested transaction - explicitly flush the session - try a SaveOrUpdate instead of Save – jbl Jan 29 '13 at 15:32
  • I moved it out so the transaction was not nested and explicitly called Flush and tried SaveOrUpdate, still no luck though. – Fabii Jan 29 '13 at 15:48
  • The project is available on github : https://github.com/Fabii23/NHibernate.git if you want to take a more in depth look. – Fabii Jan 29 '13 at 15:53
  • 1
    @Fabii opened it in vs2012, with some conversions and succeeded in running the hibernateexample console app and have some rows inserted in the sdf. Please note that the relevant sdf is the one located in bin/debug (and that it will be reinitialized on each build) – jbl Jan 29 '13 at 17:16
  • Ahhhh!!! So if the I insert a row then build, will that previous insert persist or does it disappear? If so , how do I prevent this from happening? – Fabii Jan 29 '13 at 17:40
1

Based on what @Jbl commented. Just waiting for @Jbl to post his comment as an answer so I can accept it.

Here is an exact explanation of what is happening.

Source: http://msdn.microsoft.com/en-us/library/ms233817.aspx

Source: http://blogs.msdn.com/b/vsdata/archive/2009/07/31/debugging-with-local-database-file.aspx

There’s a property “Copy to Output Directory” and the default value is “Copy if newer” (if you’re using .mdf or .mdb file, the default value is “Copy always”). You could check this MSDN document to learn what this property means. In short, the local database file will be copied to Output directory, and THAT database is the one that will get updated.

During application development, any changes made to the data (during run time within your application) are made to the database in the bin folder. For example, when you press F5 to debug your application, you are connected to the database in the bin folder. The database file in your root project folder is changed only when you edit the database schema or data by using Server Explorer, Database Explorer or other Visual Database Tools.

Fabii
  • 3,820
  • 14
  • 51
  • 92