简体   繁体   English

NHibernate-AliasToBean和关联

[英]NHibernate - AliasToBean and Associations

I have an entity which looks like this: 我有一个看起来像这样的实体:

public class MyEntity {
 public virtual int Id { get; set; }
 public virtual string Name { get; set ; }
 public virtual Role UserRole { get; set; }
}

The property "Name" is not a mapped property but "Id" and "UserRole" is. 属性“名称”不是映射的属性,而是“ Id”和“ UserRole”。 I want to populate this class using a custom criteria like this: 我想使用这样的自定义条件填充此类:

ICriteria c = Db.CreateCriteria<MyEntity>("m")
 .CreateAlias("Role", "r")
 .SetProjection(Projections.ProjectionList()
 .Add(
  Projections.SqlProjection(@"Name = case TypeId
                    when 2 then (select Name from tblOne where Id = EntityId)
                    when 4 then (select Name from tblTwo where Id = EntityId)
                    when 3 then (select Name from tblThree where Id = EntityId)
                    end", new string[] { "Name" }, new NHibernate.Type.IType[] { NHibernateUtil.String }))
                .Add(Projections.Property("m.Id"), "Id")
                .Add(Projections.Property("r.Id"), "UserRole.Id")
                .Add(Projections.Property("r.Desc"), "UserRole.Desc")
                .Add(Projections.Property("r.Permission"), "UserRole.Permission")
    )
    .SetResultTransformer(Transformers.AliasToBean<EntityPermission>());

However if I execute this it throws an exception "Could not find a setter for property '{UserRole.Id}' in class 'MyEntity'". 但是,如果执行此操作,它将引发异常“在类'MyEntity'中找不到属性'{UserRole.Id}'的设置器”。

So my question is doesn't aliastobean support associations, and if it does, what's the proper syntax? 所以我的问题是,是否没有aliastobean支持关联,如果支持,正确的语法是什么?

您应该将别名设置为“ UserRole”而不是“ Role”,因为属性的名称为“ UserRole”,而不是“ Role”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM