Could not resolve property on KeyColumn
I am getting this error when I return ByParameter
because KeyColumn
I think how can I make this work?
failed to resolve property: ParentId from: Entity.MenuItem
Entity.MenuItem.READ.ByParameter ("ParentId", 3);
code:
public static IList<T> ByParameter(String Parameter, Object Value)
{
using (var session = NHibernateHelper<T>.OpenSession())
{
var conjunction = new Conjunction();
conjunction.Add(Restrictions.Eq(Parameter, Value));
return session.CreateCriteria(typeof(T)).Add(conjunction).List<T>();
}
}
class MenuItemMap : Mapper<MenuItem>
{
public MenuItemMap()
{
Id(x => x.MenuItemId);
Map(x => x.Text);
HasMany(x => x.Children).KeyColumn("ParentId").Fetch.Select();
References(x => x.Parent).Fetch.Select();
}
}
source to share
Based on Exception
I would say that we can run into this problem in the case - that the caller looks like this:
var list = ByParameter<MenuItem>("ParentId", 123);
And since the snippet above does not show that the class MenuItem
contains a ValueType
(non-reference) ParentId property:
public class MenuItem
{
// the <id>
public virtual int MenuItemId { get; set; }
// References (many-to-one)
public virtual MenuItem Parent { get; set; }
// this seems to be not presented on MenuItem
// public virtual int ParentId { get; set; }
// HasMany (one-to-many)
public virtual IList<MenuItem> Children { get; set; }
Solution (s)
- Extend Model
We can add this to the model
public virtual int ParentId { get; set; }
And expand the display
// many-to-one is using the same column - so this will be WRITABLE
References(x => x.Parent).Fetch.Select();
// just a ValueType property - READONLY
Map(x => x.ParentId)
.Readonly() // or .Update(false).Insert(false)
;
now it will work
var list = ByParameter<MenuItem>("ParentId", 123);
- Just change the parameter
This would actually be the simplest solution ... Change the caller to target the existing mapping:
var list = ByParameter<MenuItem>("Parent.MenuItemId", 123);
Since the Parent
MenuItemId property is displayed ( <id>
or Id()
) (this is the ParentId column ) - we don't need a JOIN. NHibernate will generate the expected simple query
source to share