NHibernate.Linq troubles
I've started working with NHibernate for the first time. I've used other ORM's for many years. I've recently fallen into, and then back out of love with Linq-To-Sql. I liked the Linq syntax, but hated the way that I was not in control of my domain classes. When I read about NHibernate.Linq, I thought it would be the best of both worlds.
I've started a new project (see previous post), and I am trying to use NHibernate.Linq. My problem is as follows....
I have a class defined like this:
1: using System.Collections.Generic;
2: 3: namespace LinqToNHibernateTest.Model
4: {5: public class Category
6: {7: public virtual int Id { get; set; }
8: public virtual string Name { get; set; }
9: public virtual Category ParentCategory { get; set; }
10: public virtual IList<Category> ChildCategories { get; set; }
11: 12: public override string ToString()
13: {14: return this.Name;
15: } 16: } 17: }a table defined like this:
1: CREATE TABLE [dbo].[Category](
2: [Id] [int] IDENTITY(1,1) NOT NULL,
3: [Name] [nvarchar](100) NOT NULL,
4: [ParentId] [int] NULL,
5: CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED
6: (7: [Id] ASC
8: )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
9: ) ON [PRIMARY]
10: 11: GO
12: ALTER TABLE [dbo].[Category] WITH CHECK ADD CONSTRAINT [FK_Category_Category] FOREIGN KEY([ParentId])
13: REFERENCES [dbo].[Category] ([Id])
14: GO
15: ALTER TABLE [dbo].[Category] CHECK CONSTRAINT [FK_Category_Category]
and a mapping file defined like this:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3: assembly="LinqToNHibernateTest"
4: namespace="LinqToNHibernateTest">
5: <class name="LinqToNHibernateTest.Model.Category, LinqToNHibernateTest" table="[dbo].[Category]">
6: <id name="Id">
7: <column name="Id" not-null="true" />
8: <generator class="identity" />
9: </id>
10: <property name="Name" />
11: <many-to-one name="ParentCategory" column="ParentId" class="LinqToNHibernateTest.Model.Category, LinqToNHibernateTest" />
12: <bag name="ChildCategories" table="[dbo].[Category]">
13: <key column="ParentId" />
14: <one-to-many class="LinqToNHibernateTest.Model.Category, LinqToNHibernateTest" />
15: </bag>
16: </class>
17: </hibernate-mapping>
I'd like to get all Categories that do not have a ParentCategory. So I ran the following code:
1: var query = from s in session.Linq<Category>()
2: where s.ParentCategory != null
3: select s;But it throws the following exception:
NHibernate.QueryException: could not resolve property: of: LinqToNHibernateTest.Model.Category
Am I doing something wrong? Or is this a bug? I wrote the query in HQL, and it worked just fine:
1: 2: var query = session.CreateQuery("select cat from Category as cat where cat.ParentCategory is null").List<Category>();
3: 4: So that leads me to believe that I've found a bug in NHibernate.Linq. I am going to try to get a resolution to this, and will post an update here at a later date.
I zipped up the code as a web application project for you to download here.
Update: I posted this up at Rhino Tools Dev, and got confirmation that it is a bug.
