简体   繁体   中英

cannot insert NULL - Fluent Nhibernate Many to Many Relationship Delete Mapping

A am getting the following bug with a fluent nhibernate mapping

"Cannot insert the value NULL into column 'TitleId', table 'database.dbo.Issues'; column does not allow nulls. UPDATE fails. The statement has been terminated."

I have the Following mapping

Title table has the following columns Title.Id and a Title.TitleCrossPromotionId

CrossPromoLinkups table has PromoTitleId and TitleCrossPromoId which link to the title table in that order

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

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)

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

        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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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