NHibernate Error - “could not initialize a collect

2019-02-18 13:13发布

问题:

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:

on this line of code:

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.

回答1:

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();