开发者

Why does FluentNhibernate return NullReference for existing row?

I do not get an object from this setup

table SPEC (
  Special_Name varchar2 pk
  Signal integer
  Created_At date)

namespace App
{
    public class Strategy
    {
        pu开发者_如何转开发blic virtual string Id { get; private set; }
        public virtual int Signal { get; set; }
        public virtual DateTime CreatedAt { get; set; }
    }

    public class StrategyMap : ClassMap<Strategy>
    {
        public StrategyMap()
        {
            this.Table("SPEC");
            this.Id(x => x.Id).Column("Special_Name");
            Map(x => x.Signal).Column("Signal");
            Map(x => x.CreatedAt).Column("Created_At");
        }
    }
}

namespace App.Tests
{
    public class StrategyMapTest
    {
        private ISessionFactory sessionFactory;

        public StrategyMapTest()
        {
            this.sessionFactory = this.CreateSessionFactory();
        }

        [Fact]
        public void Can_read_from_database()
        {
            using (var session = this.sessionFactory.OpenSession())
            {
                var fromDb = session.Get<Strategy>("Foo 1");
                Assert.Equal("Foo 1", fromDb.Id);
                Assert.Equal("1", fromDb.Signal);
                Assert.Equal(new DateTime(2011,1,1), fromDb.CreatedAt);
            }
        }

        private ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(OracleClientConfiguration
                    .Oracle10
                    .ConnectionString("connString"))
                .Mappings(m => m.FluentMappings
                    .AddFromAssemblyOf<Strategy>()
                    .ExportTo(@"C:\logs"))
                .BuildSessionFactory();
        }
    }
}

I get an exception in the first assert. No object is retrieved from the database - it exists.

System.NullReferenceException: Object reference not set to an instance of an object.

My exported mapping looks like this

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="App.Strategy, App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0af8b89e104ed570" table="SPEC">
    <id access="backfield" name="Id" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Special_Name" />
      <generator class="assigned" />
    </id>
    <property name="Signal" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Signal" />
    </property>
    <property name="CreatedAt" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Created_At" />
    </property>
  </class>
</hibernate-mapping>


Did you check if any SQL query is sent to the database ? you can see that either by using a profiler on the database or the best way is by using Nhibernate Profiler (commercial but there's a free trial)

If there's no SQL sent then there is some problem with your configuration (thought at first glance it seems ok) If you do see an SQL query sent, you will probably know what your problem is by looking at it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜