简体   繁体   English

无法插入NULL-Fluent Nhibernate多对多关系删除映射

[英]cannot insert NULL - Fluent Nhibernate Many to Many Relationship Delete Mapping

A am getting the following bug with a fluent nhibernate mapping 我正在通过流畅的NHibernate映射获取以下错误

"Cannot insert the value NULL into column 'TitleId', table 'database.dbo.Issues'; column does not allow nulls. UPDATE fails. The statement has been terminated." “无法将值NULL插入表'database.dbo.Issues'的列'TitleId'中;该列不允许为空。UPDATE失败。该语句已终止。”

I have the Following mapping 我有以下映射

Title table has the following columns Title.Id and a Title.TitleCrossPromotionId 标题表具有以下列Title.IdTitle.TitleCrossPromotionId

CrossPromoLinkups table has PromoTitleId and TitleCrossPromoId which link to the title table in that order CrossPromoLinkups表具有PromoTitleIdTitleCrossPromoId ,它们以该顺序链接到标题表

(The second column if effectively the same as the Id column, I know is bad design but this is a legacy system and so changing the database will be a pain) (第二列如果实际上与Id列相同,我知道这是不好的设计,但这是旧系统,因此更改数据库将很麻烦)

This effectively makes the CrossPromoLinkups table a relation table creating a Many to Many for the Titles table (with a couple extra columns of data in it) 这有效地使CrossPromoLinkups表成为一个关系表,从而为Titles表创建了多对多表(其中包含几个额外的数据列)

Title Mapping 标题映射

        HasMany<CrossPromotionLinkUp>(c => c.PromotionLinkUp)
            .Table("dbo.CrossPromoLinkups")
            .Cascade.SaveUpdate()
            .Inverse()
            .LazyLoad()
            .KeyColumn("TitleCrossPromoId") //need to set the Id its mapped to (its not Title.Id its Title.CrossPromotionId
            .PropertyRef("TitleCrossPromotionId")
            ;


        HasMany<CrossPromotionLinkUp>(c => c.AdvertisedPromotionLinkUp)
            .Table("dbo.CrossPromoLinkups")
            .Cascade.None()
            .Inverse()
            .LazyLoad()
            .KeyColumn("PromoTitleId"); //TitleCrossPromoId");

        HasMany<Issue>(c => c.Issues)
            .Table("dbo.Issues")
            .KeyColumn("TitleId")
            .LazyLoad()
            .Cascade.SaveUpdate();

CrossPromoLinkups Mapping CrossPromoLinkups映射

        References(x => x.PromotionTitle, "PromoTitleId");
        References(x => x.TitleCrossPromotion, "TitleCrossPromoId")
            .Cascade.Delete()
            .LazyLoad()
            .PropertyRef("TitleCrossPromotionId");

Notice in the error it is complaining about "Issues" which Titles has many of. 注意在错误中,它抱怨“标题”有很多“问题”。 Something is happening where it seems to be trying to delete the Title I think when I try removing a record from CrossPromoLinkups 当我尝试从CrossPromoLinkups中删除记录时,似乎正在尝试删除标题,这是我正在考虑的事情

var PromotionLinkupToDelete = PromotionLinkUp.SingleOrDefault(x => x.Id == crossPromoLinkupId);
PromotionLinkUp.Remove(PromotionLinkupToDelete);

What have I done wrong with the mapping? 我在映射上做错了什么?

Thanks 谢谢

Because I didnt want to delete the title I needed to update the reference mapping to be the following: 因为我不想删除标题,所以需要将参考映射更新为以下内容:

 References(x => x.PromotionTitle, "PromoTitleId")
                .Cascade.None(); //dont delete the title

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM