Pass a Guid to NHibernate's ISession.Get<T> method
I'm experiencing with NHibernate 3.1.0 (without Fluent yet) on an existing solution in VS2010. Our DB is MsSql 2008 (I'm using NHibernate.Dialect.MsSql2008Dialect in the cfg.xml file).
I have an ValidationActivity object which I'd like to get from the DB. It has an ActivityID column of type uniqueidentifier (in the DB) / Guid (in the business object). I've been following Summer Of NHibernate's 1st session so my provider is:public ValidationActivity GetActivityById(Guid activityGuid)
{
var sessionFactory = new configuration().Configure().BuildSessionFactory();
var session = sessionFactory.OpenSession();
return session.Get<ValidationActivity>(activityGuid);
}
My mapping uses the guid generator:
<id name="ActivityID" type="Guid">
<generator class ="guid"/>
</id>
To test that, I created a test that tries to get an activity with a known Guid (one that I've inserted manually) as follows:
NHibernateDataProvider provider = new NHibernateDataProvider();
Guid g开发者_StackOverflow社区uid = new Guid("885f380d-c6e3-459e-8cff-10f96f26cc0a");
Activity testActivity = provider.GetActivityById(guid);
The exception thrown is 'Could not load entity' with the following SQL string:
SELECT validation0_.ActivityID as ActivityID0_0_, validation0_.ActivityName as Activity2_0_0_, validation0_.CreationDate as Creation3_0_0_, validation0_.Owner as Owner0_0_, validation0_.Project as Project0_0_, validation0_.Step as Step0_0_ FROM Activities validation0_ WHERE validation0_.ActivityID=?
It seems to me that the '?' at the end is wrong, but I don't know what am I doing wrong.
Please help. Thanks.Take a look at this post with a similar problem: Mapping Error in NHibernate
Have you tried to run the query above replacing the ? with the actual guid in the database that this test is hitting? Are all the column/table names the same? Seems like it could be a mapping issue.
精彩评论