简体   繁体   中英

Is it possible to set foreign key to null without HasMany?

I have entity Territory

    class Territory
    {
      public int Id {get;set;}
      public string Title {get;set;}
    }

and some entities which have references to it, such as

    class MyObject
    {
      public Territory Territory {get;set;}
    }

and mapping in FluentNHibernate

    References(x => x.Territory)
            .Column("TerritoryId")
            .Nullable()                
            .Not.LazyLoad()
            .Cascade.None();

and when I'm deleting territory I have error: The DELETE statement conflicted with the REFERENCE constraint "FK377ABC4DAD038F1B". The conflict occurred in database "GPM_Test", table "dbo.MyObject", column 'TerritoryId'.

How can I make nhibernate set FK to NULL before territory is deleted?

I need something like in SQL

    ON DELETE set NULL

I would say you have to delete the links between the entities first before you can delete the object itself, this can ofcourse be done in the database by cascade deletion. Another way is to programmicaly removed the links between the entity you want to remove and the related tables.

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