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 6735249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:59:13+00:00 2026-05-26T10:59:13+00:00

Extreme newbie question. I have my database (SQL Server) set up to cascade deletes

  • 0

Extreme newbie question. I have my database (SQL Server) set up to cascade deletes for my relationships so if you delete a parent entity all the children are deleted as well (ON DELETE CASCADE). I want this to be reflected in my automapped Fluent NHibernate setup. However, when I try to delete a child entity NHibernate is instead attempting to set the relationship key to NULL.

The database is super-simple: (-- for “one”, -< for “many”)

User ---< UserCode >--- Code >--- CodeGroup

When I delete a CodeGroup, the deletes should cascade down to Code and UserCode. When I delete a Code, it should just cascade to UserCode but leave CodeGroup untouched.

My entities (properties removed for clarity):

public class User {
    public virtual IList<Code> FoundCodes { get; private set; }
}
public class Code {
    public virtual IList<User> UsersWithCode { get; private set; }
    public virtual CodeGroup CodeGroup { get; set; }
}
public class CodeGroup {
    public virtual IList<Code> Codes { get; private set; }
}

Here’s how the SessionFactory looks:

var _sessionFactory =
    Fluently.Configure()
    .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("db")).ShowSql())
    .Cache(csb => csb.UseQueryCache())
    .Mappings(m => 
      m.AutoMappings.Add(
       AutoMap.AssemblyOf<Code>(new AutomappingConfiguration())
       .Override<User>(map => map.HasManyToMany(u => u.FoundCodes).Table("UserCode"))
       .Override<Code>(map => map.HasManyToMany(c => c.UsersWithCode).Inverse().Table("UserCode"))
       .Conventions.Add(new CustomForeignKeyConvention())))
    .BuildSessionFactory())

But when I do this:

using (var tx = _db.BeginTransaction())
{
    var codeGroup = _db.Load<CodeGroup>(id);
    _db.Delete(codeGroup);
    tx.Commit();
}

I get this:

could not delete collection: [MidnightRoseRace.Data.Entities.CodeGroup.Codes#8]
    [SQL: UPDATE [Code] SET CodeGroupId = null WHERE CodeGroupId = @p0]
Cannot insert the value NULL into column 'CodeGroupId', table 'MyNamespace.dbo.Code'; 
    column does not allow nulls. UPDATE fails.
The statement has been terminated.

All it has to do is delete, but instead it’s trying to set a non-nullable foreign key to null. What’s going on?

  • 1 1 Answer
  • 0 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-26T10:59:13+00:00Added an answer on May 26, 2026 at 10:59 am

    Deletes do not cascade by default in NHibernate. Set cascading on codeGroup.Codes relationship like this:

    AutoMap.AssemblyOf<Code>(new AutomappingConfiguration())
        // existing overrides
        .Override<CodeGroup>(
            map => map.HasMany(c => c.Codes).Cascade.AllDeleteOrphan().Inverse())
    

    And similarly for other relations that you need to be influenced.

    Edited by OP: Just needed that “.Inverse()” at the end. Related to this question: key-many-to-one and key-property association: nhibernate won't DELETE items from set

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

Sidebar

Related Questions

Yet another Lucene.net question by an extreme newbie to it. This time, I have
Sorry for the extreme newbie question, but I couldn't find the answer elsewhere and
Extreme Android developer newbie here...well, new to Android development, not development in general. I
Is anybody experiencing extreme slowness when using Visual Studio 2008? I have a fairly
I have a few questions regarding JavaScript obfuscation on client side. First question :
I'm an extreme newbie at C#, but I've been slowly moving through the Head
For an application I am writing, I want to have extreme extensibility and extension
I have a query returning large XML, its size can reach 1GB in extreme
Hi i have a partial website up as im having extreme difficultys trying to
I have a test server that sits in the corner of our office. I

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.