开发者

ArgumentOutOfRangeException Fluent NHibernate Export Mappings

开发者 https://www.devze.com 2023-04-01 21:19 出处:网络
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 NHi

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, ExceptionRe开发者_Python百科source 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.


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:

m => m.FluentMappings.AddFromAssemblyOf<Student>()

but it should (obviously) be:

m => m.FluentMappings.AddFromAssemblyOf<StudentMap>()

Stupid mistake, but hopefully this helps someone else.


  1. Try to remove the call to ExportTo(). There is currently a bug in Fluent NHibernate that could create wrong mappings when the files are exported.
  2. Every NHibernate entity needs an Id (primary key of the table). Create an Id property and map it.
  3. There is a NhQueryProvider in your stack trace. In the current version (3.2.0.GA) this class is named DefaultQueryProvider. 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).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号