无法解析KeyColumn上的属性

时间:2020-12-14 11:30:59

I am getting this error on return of ByParameter because of KeyColumn I guess, how can I make this work?

我在返回ByParameter时收到此错误,因为KeyColumn我想,我怎么能让这个工作?

could not resolve property: ParentId of: Entity.MenuItem

无法解析属性:ParentId: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();
    }
}

2 个解决方案

#1


Based on the Exception I would say, that we can face this issue in case - that the calling side looks like this:

根据我要说的例外情况,我们可以面对这个问题 - 调用方看起来像这样:

var list = ByParameter<MenuItem>("ParentId", 123);

And because the snippet above does not show that class MenuItem contains ValueType (non-reference) property ParentId:

并且因为上面的代码段没有显示类MenuItem包含ValueType(非引用)属性ParentId:

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)

  1. Extend model

We can add that into model

我们可以将其添加到模型中

public virtual int ParentId { get; set; }

And extend the mapping

并扩展映射

// 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)
     ;

this would work now

这会有效

var list = ByParameter<MenuItem>("ParentId", 123);
  1. Just change the param
  2. 只需改变参数

In fact, this would be the most simple solution... Change the calling side to be targeting existing mapping:

事实上,这将是最简单的解决方案......将主叫方改为目标映射:

var list = ByParameter<MenuItem>("Parent.MenuItemId", 123);

Because Parent's property MenuItemId (the <id> or Id()) is presented (it is the column ParentId) - we do not need JOIN. NHibernate will generate expectable simple query

因为显示了Parent的属性MenuItemId( 或Id())(它是ParentId列) - 我们不需要JOIN。 NHibernate将生成可预期的简单查询

#2


If you are not using conventions to change the column name in the Reference method. The default will be Parent_id, you must to specify as ParentId:

如果您没有使用约定来更改Reference方法中的列名称。默认值为Parent_id,您必须指定为ParentId:

References(x => x.Parent).Column("ParentId").Fetch.Select();

#1


Based on the Exception I would say, that we can face this issue in case - that the calling side looks like this:

根据我要说的例外情况,我们可以面对这个问题 - 调用方看起来像这样:

var list = ByParameter<MenuItem>("ParentId", 123);

And because the snippet above does not show that class MenuItem contains ValueType (non-reference) property ParentId:

并且因为上面的代码段没有显示类MenuItem包含ValueType(非引用)属性ParentId:

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)

  1. Extend model

We can add that into model

我们可以将其添加到模型中

public virtual int ParentId { get; set; }

And extend the mapping

并扩展映射

// 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)
     ;

this would work now

这会有效

var list = ByParameter<MenuItem>("ParentId", 123);
  1. Just change the param
  2. 只需改变参数

In fact, this would be the most simple solution... Change the calling side to be targeting existing mapping:

事实上,这将是最简单的解决方案......将主叫方改为目标映射:

var list = ByParameter<MenuItem>("Parent.MenuItemId", 123);

Because Parent's property MenuItemId (the <id> or Id()) is presented (it is the column ParentId) - we do not need JOIN. NHibernate will generate expectable simple query

因为显示了Parent的属性MenuItemId( 或Id())(它是ParentId列) - 我们不需要JOIN。 NHibernate将生成可预期的简单查询

#2


If you are not using conventions to change the column name in the Reference method. The default will be Parent_id, you must to specify as ParentId:

如果您没有使用约定来更改Reference方法中的列名称。默认值为Parent_id,您必须指定为ParentId:

References(x => x.Parent).Column("ParentId").Fetch.Select();