FluentNhibernate Mapping Using Index Out of Range Reference When Adding a New Object

I am trying to map the parent child relationship between the Person object and the Organization object in FluentNHibernate using the links in the mapping file as follows:

Links (x => x.Organization);

A relationship is a Person object that contains an OrganizationId column that corresponds to an entry in the Organization table with the corresponding OrganizationId. Different Person records may belong to the same Organization.

When I get a Person object, it works correctly. Person.Organization is populated. I can save the Person object and it works correctly. However, when I try to add a new Person object, I get an Index Out of Range exception from NHibernate. An exception occurs when calling session.SaveOrUpdate (person). I tried changing the display to:

Links (x => x.Organization) .Cascade.None ()

Still getting the same error. I cannot understand what causes the exception. Full exception:

System.IndexOutOfRangeException: Invalid index 22 for this SqlParameterCollection with Count = 22. in System.Data.SqlClient.SqlParameterCollection.RangeCheck (index Int32) in System.Data.SqlClient.SqlParameterCollection.GetParameter (index Int32) in System.Data.mon. DbParameterCollection.System.Collections.IList.get_Item (Int32 index) in NHibernate.Type.NullableType.NullSafeSet (IDbCommand cmd, Object value, Int32 index) in NHibernate.Type.ManyToOneType.NullSafeSet, IDbComm [] custom, ISessionImplementor session) in NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate (Object id, Object [] fields, Object rowId, Boolean [] includeProperty, Boolean [] [] includeColumns, Int32 table, IDbCommand, session ISessionImplementor Int32 index) in NHibernate.Persister.Entity.AbstractEntityPersi ster.GeneratedIdentifierBinder.BindValues ​​(IDbCommand ps) in NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert (SqlCommandInfo insertSQL, ISessionImplementor session, binder IBinder) in NHibernate.Persister.Entity.AbleE] ObjectsPerister.ertbins] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session) in NHibernate.Persister.Entity.AbstractEntityPersister.Insert (Object [] fields, Object obj, ISessionImplementor session) in NHibernate.Action.EntityIdentityInsertAction.Exnute.Exnute.exnute.exnute.execute.execute (). Execute (IExecutable executable file) in NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate (object object, EntityKey key, IEntityPersister persister, boolean useIdentityColumn, all object, IEventSource source, boolean value requireImmediateIdAccess.Nhiber) nt.Default.AbstractSaveEventListener.SaveWithGeneratedId (Object object, String entityName, Object any, source IEventSource, Boolean trebuetImmediateIdAccess) in NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId (event SaveOrUpdateEvent) in NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient (SaveOrUpdateEvent event ) in NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate (SaveOrUpdateEvent event) in NHibernate.Impl.SessionImpl.FireSaveOrUpdate (SaveOrUpdateEvent event) in NHibernate.Impl.SessionImpl.sessionImpremission

+6
nhibernate fluent-nhibernate
source share
1 answer

First of all, this exception occurs because you are matching the same column twice in your class, and this is not valid. Check it out IndexOutOfRangeException Deep in the bowels of NHibernate

Secondly (and according to your last comment), having the OrganizationId and Organization properties in your Person class, sorting against grain occurs. You do not need it. You can access it through per1.Organization.Id

If you already have an Organization in your db (e.g. Id = 5) and you map the Organization class to the Person class using the OrganizationId foreign key, then when you code

 Organization theOrganizationWithId5 = nhSession.Get<Organization>(5); per1.Organization = theOrganizationWithId5; nhSession.Save(per1); 

NHibernate simply sets the value 5 to the OrganizationId column in the Person table. You will not fill out the Organization.

+7
source share

All Articles