我有多对一的映射工作正常,但位置和location_times之间的一对多关系一直给我一个错误。
我一直收到这个错误:
在这行代码上:
映射看起来像这样:
位置:
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 =?]“}”
我可以从错误消息中的sql看到它确实在寻找locationti0_.Location_id,我知道它不存在。我不知道为什么它正在寻找那个。
答案 0 :(得分:6)
这通常是您的表上ID名称不匹配的问题。检查以确保Location在表上有一个ID列,并且它符合您的约定或正确映射。您不共享位置的地图,完整对象图或任何表,因此很难分辨ID的命名以及它们是否正确匹配。
修改强>
根据RichardD在评论中的回答,修改LocationMap如下:
public class LocationMap : ClassMap<Location>
{
public LocationMap()
{
Table("Locations");
Id(x => x.locationID).Column("ID");
HasMany(x => x.LocationTimes).KeyColumn("LID").Inverse().Cascade.All();