Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:01:38+00:00 2026-05-23T03:01:38+00:00

I have two entities, EntityA and EntityB. EntityB can optionally be related to EntityA,

  • 0

I have two entities, EntityA and EntityB. EntityB can optionally be related to EntityA, and has a foreign key column that is nullable. When EntityA is deleted, I want the foreign key in EntityB to be set to null.

So when I delete EntityA, I’d like it to set the foreign key in any referenced EntityBs before issuing the DELETE command. I’ve seen NHibernate do this before “accidentally”, and now that I actually want it to do that, I can’t seem to get it right. The best I’ve been able to do is delete EntityB once it is orphaned, which is not what I want. Of course, I could set the foreign key constraint in SQL Server to set the column to null, but I feel like NHibernate should be able to do this. Similarly, I could loop through all EntityB referenced by the EntityA to be deleted, and set them to null. Again, why should I have to?

Here are my entities and mappings:

public class EntityA : Entity // Entity contains the ID property and implements GetHashCode() and Equals()
{
    // irrelevant properties

    public virtual IList<EntityB> EntityBs { get; protected set; }
}

public class EntityB : Entity
{
    // irrelevant properties
    public virtual EntityA EntityA { get; set; }
}


public class EntityAMap: ClassMap<EntityA>
{
    public EntityAMap()
    {
         HasMany(c => c.EntityBs)
             .ForeignKeyConstraintName("FK_EntityA_EntityB")
         .KeyColumn("EntityA_Id")
             .Inverse()
             .Cascade.None(); // i've tried other cascade settings with little luck
    }   
}

public class EntityBMap: ClassMap<EntityB>
{
    public EntityBMap()
    {
         References(c => c.EntityA)
             .Nullable()
             .Column("EntityA_Id")
             .ForeignKey("FK_EntityA_EntityB")
             .Index("IX_EntityB_EntityA_Id")
             .Cascade.None();
    }   
}

I then delete like this:

var entityA = Repository.Get(idToDelete);
// entityA.EntityBs.Clear(); // tried this, no affect
Repository.Remove(entityA);

Repository.Flush(); // is it possible this isn't working because I'm missing a Tx?

The result is a foreign key constraint violation, because EntityB.EntityA_Id is not being nulled out.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:01:39+00:00Added an answer on May 23, 2026 at 3:01 am

    You need to do something like:

    entityA.EntityBs.ToList().ForEach(b => b.EntityA = null );
    
    Repository.Remove(entityA);
    Repository.Flush();
    

    If any B’s are still referencing A you can’t delete A, especially with no cascading going on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities: EntityA and EntityB. EntityB has a foreign field of EntityA:
I have two entities // All details has been removed @Entity @Table(name = "A_TABLE_NAME")
Consider that i have two entities with following relationship: Entity A <-->> Entity B
I have two entities. Groups. Pools. A Group can create many pools. So I
I have two entities: User and Event. Each user has a list of events
Imagine you have two entities, Player and Team , where players can be on
I have two entities in Doctrine 2.1: Category and Site each category has many
I have two related entities, say @Entity public class Book { @ManyToOne Shelf shelf;
I have two tables/entities client and site that have a many to many relationship
I have two entities event and time. The event entity has a 1 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.