NHibernate Error - "Failed to initialize collection"

I have many-one mappings working fine, but the one-to-many relationship between locations and location_times continues to give me an error.

I keep getting this error: enter image description here

in this line of code: enter image description here

The mappings are as follows:

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] 

Full error message:

"{" failed to initialize the 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__Hart1_0Start0_.hart FROM Location_Times locationti0_ WHERE locationti0_.Location_id =?] "}"

I see from sql in the error message that it is really looking for locationti0_.Location_id, which I know does not exist. I don’t know why he is looking for it.

+4
source share
1 answer

This is usually a problem of incorrect identifier matches in your tables. Make sure that there is an identifier column in the table for the location and that it follows your agreement or is displayed correctly. You do not share a location map, a complete graphic, or any of the tables, so it is difficult to determine which names are named and whether they match correctly.

Edit:

In the RichardD report in the comments, change the LocationMap 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(); 
+6
source

Source: https://habr.com/ru/post/1392995/


All Articles