5

I have many-to-one mappings working fine, but a one-to-many relationship between locations and location_times keeps giving me an error.

I keep getting this error: enter image description here

on this line of code: enter image description here

Mappings look like this:

Location:

public virtual IList<LocationTimes> LocationTimes { get; set; }

    public virtual int locationID { get; set; }
    public virtual IList<LocationTimes> LocationTimes { get; set; }

    public Location()
    {
        LocationTimes = new List<LocationTimes>();
    }

Location Map:

 public class LocationMap : ClassMap<Location>
 {
    public LocationMap()
    {
        Table("Locations");

        Id(x => x.locationID).Column("ID");    

        HasMany(x => x.LocationTimes)
          .Inverse()
          .Cascade.All();   

Location Table:

CREATE TABLE [dbo].[Locations](
    [ID] [int] IDENTITY(1,1) NOT NULL
    ...
    CONSTRAINT [PK_Locations_1] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

LocationTimes:

public class LocationTimes
{
    public virtual int ID { get; set; }
    public virtual Location Location { get; set; }   
}

LocationTimesMap:

public class LocationTimesMap : ClassMap<LocationTimes>
{
    public LocationTimesMap()
    {
        Table("Location_Times");

        Id(x => x.ID);
        References(x => x.Location).Column("LID"); 
    }
}

Location_times table:

CREATE TABLE [dbo].[Location_Times](
[ID] [int] IDENTITY(1,1) NOT NULL,
[LID] [int] NULL,
    [EHStart] [int] NULL,
[EHEnd] [int] NULL,
    [EHSell] [money] NULL,
    CONSTRAINT [PK_Location_Times_1] PRIMARY KEY CLUSTERED 
    (
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

The full error message:

"{"could not initialize a collection: [WhygoDomain.Location.LocationTimes#4] [SQL: SELECT locationti0_.Location_id as Location4_1_, locationti0_.ID as ID1_, locationti0_.ID as ID1_0_, locationti0_.LID as LID1_0_, locationti0_.EHStart as EHStart1_0_ FROM Location_Times locationti0_ WHERE locationti0_.Location_id=?]"}"

I can see from the sql in the error message that it is indeed looking for locationti0_.Location_id, which I know doesn't exist. I don't know why it's looking for that though.

Positonic
  • 9,151
  • 14
  • 57
  • 84
  • 1
    Just check if you have all of those columns in your DB. one (or more) is missing – gdoron Jan 25 '12 at 18:18
  • @gdoron thank you for your suggestions. I have removed all unnecessary fields now. I can see from the sql in the error message that it is indeed looking for locationti0_.Location_id, which I know doesn't exist. I don't know why it's looking for that though... – Positonic Jan 26 '12 at 10:10
  • Are you using some sort of conventions? – gdoron Jan 26 '12 at 10:18
  • Possible duplicate of [NHibernate exception: could not initialize a collection, Invalid column name. Fluent mapping. Maybe a many-to-one issue?](http://stackoverflow.com/questions/7681928/nhibernate-exception-could-not-initialize-a-collection-invalid-column-name-fl) – Frédéric Apr 05 '17 at 13:07

1 Answers1

6

This is usually a problem of ID name mis-matches on your tables. Check to ensure that Location has an ID column on the table and that it follows your convention or is mapped correctly. You don't share Location's map, full object graph, or any of the tables so its hard to tell what the IDs are named and if they are matching up correctly.

Edit:

Per RichardD's answer in the comments, modify the LocationMap to be as follows:

public class LocationMap : ClassMap<Location>
 {
    public LocationMap()
    {
        Table("Locations");
        Id(x => x.locationID).Column("ID");
        HasMany(x => x.LocationTimes).KeyColumn("LID").Inverse().Cascade.All();
Fourth
  • 9,163
  • 1
  • 23
  • 28
  • Thanks for your answer, I have double check a few things and provided more info above. I'm glad you say it's a usual problem, it makes me feel a bit better about this ;) Do you have any further ideas? – Positonic Jan 25 '12 at 18:41
  • Edited my answer given your table defs. – Fourth Jan 25 '12 at 18:57
  • Thank you. I have tried your suggested changes and still get the same error. It still seems to be generating Location_id in the SQL. I have updated above with my latest, can you see any other reason why it would doing this? So frustrating... – Positonic Jan 26 '12 at 09:47
  • @iKode Try changing `References(x => x.Location).Column("LID");` to `References(x => x.Location, "LID");` – Richard Dalton Jan 26 '12 at 09:49
  • 1
    @iKode Thought it might, think it's actually the other side of the relationship. Add `.KeyColumn('LID')` to the HasMany in `LocationMap` like this - `HasMany(x => x.LocationTimes).KeyColumn("LID").Inverse().Cascade.All();` – Richard Dalton Jan 26 '12 at 10:28
  • @RichardD that's exactly what I needed. I'm so greatful for your help! Thank you so much!!! – Positonic Jan 26 '12 at 15:17