我有与文章类有多对多关系的标签类,问题是我想制作一个在标签视图模型类中表示的投影,所以列结果应该是这样的
Id|Name|CreatedBy|CreatedDate|LastModifiedBy|LastModifiedDate|ArticlesCount
SQL 查询应该是这样的:
SELECT t.*, COUNT(at.intTag) as ArticlesCount
FROM dbo.TAG t
LEFT OUTER JOIN dbo.ARTICLE_TAG at ON t.intID = at.intTag
LEFT OUTER JOIN dbo.ARTICLE a ON at.intID = a.intID
GROUP BY t.intID, t.vcName, t.intWeight, t.vcCreatedBy, t.dtCreated, t.vcLastMod, t.dtLastMod, t.btActive
但它说
NHibernate.Exceptions.GenericADOException was caught
HResult=-2146232832
Message=could not execute query [ SELECT this_.intID as y0_, this_.vcName as y1_, this_.vcCreatedBy as y2_, this_.dtCreated as y3_, this_.vcLastMod as y4_, this_.dtLastMod as y5_, count(this_.intID) as y6_ FROM TB_TAG this_ WHERE this_.btActive = @p0 ]
Name:cp0 - Value:True
这是我的标签类:
public class Tag {
public virtual string Name { get; set; }
public virtual int Weight { get; set; }
public virtual IList<Article> Articles { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string LastModifiedBy { get; set; }
public virtual DateTime LastModifiedDate { get; set; }
public virtual bool IsActive { get; set; }
}
这是映射类
internal sealed class TagMap : ClassMap<Tag>
{
public TagMap()
{
Table("TB_TAG");
Id(f => f.Id).Column("intID").GeneratedBy.Native();
Map(f => f.Name).Column("vcName").Not.Nullable();
Map(f => f.Weight).Column("intWeight").Not.Nullable();
Map(f => f.IsActive).Column("btActive");
Map(f => f.CreatedBy).Column("vcCreatedBy").Not.Update();
Map(f => f.CreatedDate).Column("dtCreated").Not.Update();
Map(f => f.LastModifiedBy).Column("vcLastMod");
Version(f => f.LastModifiedDate).Column("dtLastMod");
HasManyToMany(f => f.Articles).Table("S_ARTICLE_TAG")
.ParentKeyColumn("intTag").ChildKeyColumn("intID")
.Inverse();
}
}
这是 ViewModel 类:
public class TagView
{
public int Id { get; set; }
public string Name { get; set; }
public int ArticlesCount { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string LastModifiedBy { get; set; }
public virtual DateTime LastModifiedDate { get; set; }
public virtual bool IsActive { get; set; }
}
这是我进行投影的代码:
Tag t = null;
tags = _session.QueryOver(() => t)
.Select(Projections.Id().As("Id"),
Projections.Property(() => t.Name).As("Name"),
Projections.Property(() => t.CreatedBy).As("CreatedBy"),
Projections.Property(() => t.CreatedDate).As("CreatedDate"),
Projections.Property(() => t.LastModifiedBy).As("LastModifiedBy"),
Projections.Property(() => t.LastModifiedDate).As("LastModifiedDate"),
Projections.Count(() => t.Articles).As("ArticlesCount"))
.TransformUsing(Transformers.AliasToBean<TagView>())
.List<TagView>();
这是我第一次使用投影,我不知道如何让它工作。我在这里错过了什么吗?