禁止代码映射不匹配XML映射

时间:2018-05-16 17:36:48

标签: c# nhibernate nhibernate-mapping

我正在尝试将我的nhibernate xml映射转换为代码映射。

我的MSSQL数据库中有一个名为characterMedia的表。

内部characterMedia是2列:

characterId = PK, FK, bigint, not null 
mediaId = PK, FK, bigint, not null

可能存在重复的characterIds,但没有重复的mediaIds ...因此相同的字符可以包含许多mediaId记录。

这是我的工作XML映射:

<bag name="Media" table="characterMedia" cascade="all">
  <key column="characterId" />
  <many-to-many column="mediaId" class="Game.Storage.GameMedia" />
</bag>

这是我的代码映射:

        //begin character media
        Bag(x => x.Media, cm => {
            cm.Schema("game");
            cm.Table("characterMedia");
            cm.Key(km => km.Column("characterId"));
            cm.Cascade(Cascade.All);
        }, rm => {
            rm.ManyToMany(m => m.Column("mediaId"));
        });
        //end character media 

每当我运行游戏时,代码映射都会生成nullExceptions。但XML映射没有。

我在代码映射中遗漏了什么吗?

以防万一,这是我的characterMedia表创建脚本:

CREATE TABLE [game].[characterMedia](
    [characterId] [bigint] NOT NULL,
    [mediaId] [bigint] NOT NULL,
 CONSTRAINT [pk_characterMedia] PRIMARY KEY CLUSTERED 
(
    [characterId] ASC,
    [mediaId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [game].[characterMedia]  WITH CHECK ADD  CONSTRAINT [fk_characterMedia_characters] FOREIGN KEY([characterId])
REFERENCES [game].[characters] ([characterId])
ON DELETE CASCADE
GO

ALTER TABLE [game].[characterMedia] CHECK CONSTRAINT [fk_characterMedia_characters]
GO

ALTER TABLE [game].[characterMedia]  WITH CHECK ADD  CONSTRAINT [fk_characterMedia_userMedia] FOREIGN KEY([mediaId])
REFERENCES [media].[userMedia] ([id])
ON DELETE CASCADE
GO

ALTER TABLE [game].[characterMedia] CHECK CONSTRAINT [fk_characterMedia_userMedia]
GO

谢谢!

0 个答案:

没有答案