Fabii
Fabii

Reputation: 3890

Why is ISession save not inserting?

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>

Upvotes: 1

Views: 1442

Answers (2)

jbl
jbl

Reputation: 15433

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

Upvotes: 2

Fabii
Fabii

Reputation: 3890

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.

Upvotes: 1

Related Questions