NHibernate的错误 - "无法初始化集合" [英] NHibernate Error - "could not initialize a collection"

查看:184
本文介绍了NHibernate的错误 - "无法初始化集合"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多一对一的映射工作正常,但地点和location_times之间有一个一对多的关系一直给我一个错误。

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:

映射是这样的:

地点:

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

位置图:

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

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

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

位置表:

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表:

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]

完整的错误消息

{无法初始化集合:[WhygoDomain.Location.LocationTimes#4]
[ SQL:SELECT locationti0_.Location_id为Location4_1_,locationti0_.ID为ID1_,locationti0_.ID为ID1_0_,locationti0_.LID为LID1_0_,locationti0_.EHStart作为EHStart1_0_
FROM Location_Times locationti0_
其中locationti0_.Location_id =]? }

"{"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=?]"}"

我可以从错误消息中的SQL它确实找locationti0_.Location_id,我知道没有按看T存在。我不知道为什么它寻找那虽然。

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.

推荐答案

这通常是ID名称错配的问题,你的表。检查以确保位置对表中的ID列,它遵循的公约或正确映射。你不分享位置的地图,完整的对象图,或任何表,因此其很难说的ID命名,以及它们是否正确地匹配起来。

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.

修改

每RichardD在评论的答案,修改LocationMap是如下:

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

这篇关于NHibernate的错误 - &QUOT;无法初始化集合&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆