tags:

views:

32

answers:

1

It looks like all of my mappings are compiling correctly, I'm able to validly get a session from session factory. However, when I try to ISession.SaveOrUpdate(obj); I get this. Can anyone please help point me in the right direction?

private Configuration configuration;
protected Configuration Configuration {
    get
    {
        configuration = configuration ?? GetNewConfiguration();
        return configuration;
    }

}

protected ISession GetNewSession() {
    var sessionFactory = Configuration.BuildSessionFactory();
    var session = sessionFactory.OpenSession();
    return session; 

}

[TestMethod] public void
TestSessionSave() {
    var company = new Company();
    company.Name = "Test Company 1";

    DateTime savedAt = DateTime.Now;
    using (var session = GetNewSession())
    {
        try
        {
            session.SaveOrUpdate(company);
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    Assert.IsTrue((company.CreationDate != null && company.CreationDate > savedAt), "Company was saved, creation date was set."); }

For those that might be interested, here is my mapping for this class:

  <!-- Company -->
  <class name="MyNamespace.Company,MyLibrary" table="Companies">
    <id name="Id" column="Id">
      <generator class="native" />
    </id>

    <property name="ExternalId" column="GUID" generated="insert" />
    <property name="Name" column="Name" type="string" />
    <property name="CreationDate" column="CreationDate" generated="insert" />
    <property name="UpdatedDate" column="UpdatedDate" generated="always" />
  </class>
  <!-- End Company -->

Finally, here is my config -- I'm just connecting to a SQL Server CE instance for this for testing purposes.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source="D:\Build\MyProject\Source\MyProject.UnitTests\MyProject.TestDatabase.sdf"</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
  </session-factory>
</hibernate-configuration>

Thanks!

A: 

It might be a problem retrieving the identity value.

Are you sure Id is declared as identity in the DB? That's what you're telling NHibernate when declaring <generator class="native" />.

If this is a new database, it would also be useful to use a client-side generator, like guid.comb or hilo.

One more thing about your code. This:

catch (Exception e)
{
    throw e;
}

Is harmful, because all it does is swallow the stack trace, with might provide additional debugging information.

Diego Mijelshon
Thanks, Diego. I was just catching/throwing so I could debug on the exception...was not letting me see the exception otherwise.Since this is a generated DB (it's a SQL Server CE DB that I'm exporting via SchemaExport) shouldn't it set that up itself?
Brad Heller
Yes, it should. But you should (1) use transactions and (2) use Save instead of SaveOrUpdate. Also, generating properties in the server is not performant, you should use event listeners instead.
Diego Mijelshon
Fantastic, thanks Diego, that seems to do the trick..however now I'm getting the following exception: "An overflow occurred while converting to datetime."
Brad Heller
You are probably sending an "empty" DateTime down the wire (1/1/0001).
Diego Mijelshon