命名查询未知尝试使用Fluent NHibernate调用存储过程时出错
我正在为一个项目设置NHibernate,我有几个问题,由于它们的复杂性,我们将作为存储过程离开。我希望能够使用NHibernate调用存储过程,但遇到了一个我无法理解的错误。因为我使用的是Fluent NHibernate,所以我使用的是推荐的混合模式映射。然而,当我运行这个应用程序时,我得到了一个“命名查询未知:AccountsGetSingle”异常,我不知道为什么。我想我的HBM映射可能有问题,因为我不太熟悉使用它们,但我不确定 我的NHibernate配置代码是:命名查询未知尝试使用Fluent NHibernate调用存储过程时出错,nhibernate,fluent-nhibernate,nhibernate-mapping,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,我正在为一个项目设置NHibernate,我有几个问题,由于它们的复杂性,我们将作为存储过程离开。我希望能够使用NHibernate调用存储过程,但遇到了一个我无法理解的错误。因为我使用的是Fluent NHibernate,所以我使用的是推荐的混合模式映射。然而,当我运行这个应用程序时,我得到了一个“命名查询未知:AccountsGetSingle”异常,我不知道为什么。我想我的HBM映射可能有问题,因为我不太熟悉使用它们,但我不确定 我的NHibernate配置代码是: private IS
private ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString((conn => conn.FromConnectionStringWithKey("CIDB")))
.ShowSql())
.Mappings(m =>
{
m.HbmMappings.AddFromAssemblyOf<Account>();
m.FluentMappings.AddFromAssemblyOf<Account>();
})
.BuildSessionFactory();
}
私有ISessionFactory CreateSessionFactory()
{
流畅地返回。Configure()
.数据库(MsSqlConfiguration.MsSql2005
.ConnectionString((conn=>conn.FromConnectionStringWithKey(“CIDB”))
.ShowSql())
.Mappings(m=>
{
m、 HbmMappings.AddFromAssemblyOf();
m、 FluentMappings.AddFromAssemblyOf();
})
.BuildSessionFactory();
}
我的hbm.xml文件是:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="AccountsGetSingle">
<return alias="Account" class="Core, Account"></return>
exec AccountsGetSingle
</sql-query>
</hibernate-mapping>
exec AccountsGetSingle
我调用存储过程的代码如下所示:
public Account Get()
{
return _conversation.Session
.GetNamedQuery("AccountsGetSingle")
.UniqueResult<Account>();
}
公共帐户Get()
{
return\u conversation.Session
.GetNamedQuery(“AccountsGetSingle”)
.UniqueResult();
}
如有任何想法或想法,将不胜感激。谢谢
更新:@kibbled\u bits的建议让我得到了我想要的最终结果(能够从NHibernate调用存储过程),但我仍然不知道上面列出的方法为什么不起作用。我仍然很好奇为什么,因为它可能会提供对未来问题的有价值的见解。当我必须使用存储过程时(只有当我被迫使用存储过程时才会发生)。我更愿意使用以下方法来执行它们:
var list = Session.CreateSQLQuery("exec GetCustomerByNaturalKey ?, ?")
.AddEntity(typeof(Customer))
.SetInt32(0, customerNo)
.SetDateTime(1, createdDate)
.List<Customer>();
var list=Session.CreateSQLQuery(“exec GetCustomerByNaturalKey?,?”)
.附录(类型(客户))
.SetInt32(0,customerNo)
.SetDateTime(1,createdDate)
.List();
.SetInt32/DateTime的第一个参数只是该参数的顺序位置。我收到了相同的错误消息,解决该问题的方法是确保我的hbm.xml文件属性“Build action”设置为“Embedded Resource”,因此您可能想再试一次。我多次被此错误所困扰 另外两个问题可能导致这种情况 不添加hbm映射 用流利的语言我有以下几点
var config = Fluently.Configure()
.Database(sqlConfig)
.Mappings(c => c.AutoMappings.Add(AutoMap.AssemblyOf<AdvertView>(new QueryAutomappingConfiguration()).UseOverridesFromAssemblyOf<AdvertViewMappingOverride>()))
.Mappings(c => c.HbmMappings.AddClasses(typeof(AdvertView), typeof(RelatedAdvertView), typeof(CompanyAtoZListingView)));
还要检查hbm文件以确保参数正确 我想我不知道可以使用.CreateSQLQuery()调用存储过程,尽管这很有道理。早上上班的时候我会试一试,效果很好。我能够在没有任何问题的情况下成功调用存储过程。谢谢。+1份很棒的酱汁。其他答案包括映射hbm片段。这样更好。我尽可能地避免这个过程,所以很少需要这样做,我总是忘记这样做。
.Mappings(c => c.HbmMappings.AddClasses(typeof(AdvertView), typeof(RelatedAdvertView), typeof(CompanyAtoZListingView)));