ArgumentOutOfRangeException 流畅的 NHibernate 导出映射
首先,我对使用 NHibernate 和 Fluent NHibernate 非常陌生,所以我很可能犯了一个简单的错误。在过去的几天里,我从一开始就使用 Fluent NHibernate 直接进入了 NHibernate,所以我也没有任何处理 NHibernate XML 映射文件的经验。
我有一个简单的 Student 对象,它有一些属性,如姓名、出生日期、性别、版本等。第一步是从数据库中检索最大版本号。因为我只想让它工作,所以我只映射了 1 个属性。
我在这里创建 SessionFactory:
sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(
Settings.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Student>()
.ExportTo(@"C:\Temp"))
.BuildSessionFactory();
我在这里映射 Student(正如我提到的,目前我只是尝试完成第一步,即检索最大版本号。我目前没有映射学生上的其他属性)下面正是我的映射类的样子。):
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Table("StudentVersion");
Map(x => x.Version).Column("Version");
}
}
我在这里进行 NHibernate 调用:
public long GetMaxVersion()
{
using (ISession session = sessionFactory.OpenSession())
{
long maxVersion = 0;
if(session.Query<Student>().Any())
maxVersion = session.Query<Student>().Max(s => s.Version);
return maxVersion;
}
}
我也尝试过这个:
public long GetMaxVersion()
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Query<Student>().Max(s => s.Version);
}
}
作为旁注,回顾一下我的 BuildSessionFactory() 调用,我问 Fluent NHibernate 将 xml 映射导出到 C:\Temp 文件夹,但是无论我要求它导出到哪个文件夹(是的,安全设置正确),这根本不会发生。我只是猜测发生这种情况是因为 Fluent NHibernate 甚至无法完成映射?我不知道。
不管怎样,每当我对 NHibernate 进行任何一个调用时,我都会遇到以下异常:
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
at NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 98
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 28
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 103
at System.Linq.Queryable.Max(IQueryable`1 source, Expression`1 selector)
at Integration.Data.MaxVersion.StudentMaxVersionRetriever.GetMaxVersion()
我花了过去 4 小时进行谷歌搜索,几乎所有我发现的内容都表明,当两个属性映射到相同的列名时,就会发生这种情况。我未能成功导出 NHibernate 映射 XML 文件,但我看不出在我的情况下会出现这种情况。我目前只映射 1 个属性,并且没有使用任何自动映射,因此,根据我的理解,NHibernate 应该只尝试将 1 个属性映射到 1 列,仅此而已。
预先感谢任何帮助。
First off, I am extremely new with using NHibernate and Fluent NHibernate so it's very likely that I'm making a simple mistake. I jumped right into NHibernate over the last few days using Fluent NHibernate right from the beginning so I also don't have any experience dealing with NHibernate XML mapping files.
I have one simple Student object that has a few properties like Name, BirthDate, Gender, Version, etc. The first step is to retrieve the max version number from the database. Because I just want to get this working, I've only mapped that 1 property.
I'm creating the SessionFactory here:
sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(
Settings.ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Student>()
.ExportTo(@"C:\Temp"))
.BuildSessionFactory();
I'm mapping the Student here (As I mentioned, currently I'm just trying to complete the first step which is retrieving the max version number. I'm currently not mapping the other properties on the object. Below is exactly what my mapping class looks like.):
public class StudentMap : ClassMap<Student>
{
public StudentMap()
{
Table("StudentVersion");
Map(x => x.Version).Column("Version");
}
}
I'm making the NHibernate call here:
public long GetMaxVersion()
{
using (ISession session = sessionFactory.OpenSession())
{
long maxVersion = 0;
if(session.Query<Student>().Any())
maxVersion = session.Query<Student>().Max(s => s.Version);
return maxVersion;
}
}
I've also tried just this:
public long GetMaxVersion()
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Query<Student>().Max(s => s.Version);
}
}
As a side note, looking back at my BuildSessionFactory() call, I'm asking Fluent NHibernate to export the xml mappings to the C:\Temp folder, but this is not happening at all, no matter what folder I ask it to export to (yes, security is set correctly). I'm just guessing this is happening because Fluent NHibernate can't even finish the mappings? I dunno.
Anyways, whenever I make either of the calls to NHibernate, I get the following exception:
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
at NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 98
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 28
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 103
at System.Linq.Queryable.Max(IQueryable`1 source, Expression`1 selector)
at Integration.Data.MaxVersion.StudentMaxVersionRetriever.GetMaxVersion()
I've spent the last 4 hours Googling and virtually EVEYRTHING that I find states that this occurs when two properties are mapped to the same column name. I've been unsuccessful in exporting the NHibernate mapping XML files, but I can't see how this would be the case in my situation. I'm currently only mapping 1 property and I'm not using any automapping so, from my understanding, NHibernate should only try to map 1 property to 1 column and that's it.
Thanks in advance to any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,正如我所怀疑的那样,事实证明这对我来说是一个非常简单的错误。令人沮丧的是,我没有收到任何有意义的错误消息,但无论如何。
在构建会话工厂时,我打电话给:
但它(显然)应该是:
愚蠢的错误,但希望这对其他人有帮助。
Well, as I suspected, it turned out to be a VERY simple mistake on my part. It's frustrating that I wasn't receiving an error message that made any sort of sense, but whatever.
When building the session factory, I was calling:
but it should (obviously) be:
Stupid mistake, but hopefully this helps someone else.
ExportTo()
的调用。 Fluent NHibernate 当前存在一个错误,可能会在导出文件时创建错误的映射。NhQueryProvider
。在当前版本 (3.2.0.GA) 中,此类名为DefaultQueryProvider
。您可以尝试更新到当前版本。那么你必须使用 Fluent NHibernate 的 trunk 版本(发布版本尚未更新到 NHibernate 3.2)。ExportTo()
. There is currently a bug in Fluent NHibernate that could create wrong mappings when the files are exported.NhQueryProvider
in your stack trace. In the current version (3.2.0.GA) this class is namedDefaultQueryProvider
. You could try updating to the current version. Then you have to use the trunk version of Fluent NHibernate (release version is not updated to NHibernate 3.2 yet).