bcsanches
bcsanches

Reputation: 2372

When loading NHibernate array collection, the first element is null

I have a collection mapped to an array and it appears to work fine. But when I load the itens from database, the first element of the array is always null. Is there a way to avoid this?

The mapping of the owner of the array:

public class ConsistMap : BaseEntityMap<Consist>
    {
        public ConsistMap():
            base("consist_id")
        {
            Not.LazyLoad();

            Map(x => x.RemoveEmptyCars).Not.Nullable();

            HasMany(x => x.ConsistVehicles)
                .Inverse()
                .AsArray<int>(x => x.Position)
                .Cascade.AllDeleteOrphan()
                .OrderBy("position asc")
                .KeyColumn("consist_id");
        }
    }

And the elements mapping:

public class ConsistVehicleMap : BaseEntityMap<ConsistVehicle>
    {
        public ConsistVehicleMap():
            base("consist_vehicle_id")
        {
            Not.LazyLoad();

            Map(x => x.Position).Not.Nullable();

            References(x => x.Consist).Not.Nullable();

            HasMany(x => x.Tickets)
                .Inverse()
                .Cascade.None()
                .KeyColumn("consist_vehicle_id");

            References(x => x.Vehicle)
                .Column("vehicle_id")
                .Not.Nullable();
        }
    }

Any ideas why when the Consist object is created the first element of the ConsistVehicles array is null? I checked the data on the database and the only rows on the table are the elements stored on the array.

Thanks!

Upvotes: 0

Views: 534

Answers (1)

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

Just a guess: you are probably starting Position at 1 instead of 0.

Upvotes: 1

Related Questions