3

我使用ISession.CreateSQLQuery.

然后我用

SetResultTransformer(new AliasToBeanResultTransformer(typeof(Article))).List<Article>().ToList()

这种方法的问题在于,只有将表一对一地AliasToBeanResultTransformer映射到类。ArticleArticle

public class Article : Entity
{
    public virtual string Description { get; set; }
}

public class Entity
{
    public virtual int Id { get; set; }
}

public class ArticleRepository : Repository<Article>, IArticleRepository
{
    private ISession _session;

    public ArticleRepository(ISession session) : base(session)
    {
        _session = session;
    }

    public List<Article> GetByDescription(string description)
    {
        return _session
            .CreateSQLQuery("EXEC ArticlesByDescription :Description")
            .SetString("Description", description)
            .SetResultTransformer(new AliasToBeanResultTransformer(typeof(Article)))
            .List<Article>().ToList();
    }
}

但是我Article表上的主键被调用ArticleId,因此AliasToBeanResultTransformer引发异常。

在“Core.DomainModels.Article”类中找不到属性“ArticleId”的设置器

有没有办法在使用时重用 FluentNhibernateMapping CreateSqlQuery

编辑:

Nhibernate 文档描述了如何将已映射的实体与 hbm 文件一起使用。

<sql-query name="GetProductsByCategoryId">
   <return class="Product" />
   exec dbo.GetProductsByCategoryId :CategoryId
</sql-query>

我真的问自己为什么不能仅仅通过代码来做到这一点?!

4

2 回答 2

4

我正在使用此代码映射到存储库中的实体。我相信它也适用于调用程序:

public IEnumerable<Address> Search(string query, int maxCount)
{
    return session.CreateSQLQuery("SELECT * FROM address WHERE fts_col @@ plainto_tsquery('cs', :query) LIMIT :limit;")
        .AddEntity(typeof(Address)) // this is probably what you need to map to entity
        .SetString("query", query)
        .SetInt32("limit", maxCount)
        .List<Address>();
}
于 2013-09-09T18:04:23.760 回答
3

似乎无法使用ISession.CreateSQLQuery并从.ISession

我现在要做的是一起使用 hbm 文件和 FluentNhibernate 映射。

血红蛋白:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.DomainModels">
  <sql-query name="ArticlesByDescription">
    <return class="Article" />
    EXEC ArticlesByDescription :Description
  </sql-query>
</hibernate-mapping>

流畅的休眠:

public class ArticleMapping : ClassMap<Article>
{
    public ArticleMapping()
    {
        Id(x => x.ArticleId).GeneratedBy.Identity();
        Map(x => x.Description).UniqueKey("Article_Description_Unique");
    }
}

配置:

public class ConfigurationFactory
{
    const string Database = "";
    const string Server = "";

    public static Configuration Build()
    {           
        return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                c => c.Database(Database).TrustedConnection().Server(Server)))
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssemblyOf<ArticleMapping>();
                m.HbmMappings.AddFromAssemblyOf<ArticleMapping>();
            })
            //.ExposeConfiguration(c => new SchemaExport(c).Execute(true, true, false))
            .BuildConfiguration();
    }
}

存储库:

public class ArticleRepository : Repository<Article>, IArticleRepository
{
    private ISession _session;

    public ArticleRepository(ISession session) : base(session)
    {
        _session = session;
    }

    public List<Article> GetByDescription(string description)
    {
        return _session
            .GetNamedQuery("ArticlesByDescription")
            .SetString("Description", description)
            .List<Article>().ToList();
    }
}
于 2012-04-13T13:04:14.647 回答