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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:00:05+00:00 2026-05-12T18:00:05+00:00

Update: It appears that changing my mapping from Cascade.All() to Cascade.AllDeleteOrphan() fixes most of

  • 0

Update:
It appears that changing my mapping from Cascade.All() to Cascade.AllDeleteOrphan() fixes most of my issues. I still have to explicitly set the Company property on the OperatingState, which seems unnecessary as it’s being added to the Company entity, but at least I can work with that during an update. I still need to test that with a create.

If any one can explain that, that would be a big help.

Update 2: After playing with it some more, it appears I don’t always have to specify the parent entity.

Original Post

I have 2 related entities

public class Company {
        //... fields
        public virtual IList<OperatingState> OperatingStates { get; set; }
}

public class OperatingState {
        public virtual Company Company { get; set; }// Mapped on CompanyID
        public virtual string State { get; set; }
}

And they are mapped like this:

 public class CompanyMap : ClassMap<Company> {
        public CompanyMap() {
        //... fields 
          HasMany(x => x.OperatingStates)
                .Cascade.All()
                .Table("OperatingState");
        }
}
 public class OperatingStateMap : ClassMap<OperatingState> {
        public OperatingStateStateMap() {
            Id(x => x.ID);
            References(x => x.Company);
            Map(x => x.State);
        }
 }

So all is well until I try to update Company with new Operating States

Company company = _repo.GetSingle(123);
 company.OperatingStates.Clear();
 foreach(string state in form["OperatingStates"].Split(',')) {
    company.OperatingStates.Add(new OperatingState(state));
 }
 _repo.Save(company); // calls ISession.SaveOrUpdate

It bombs out with:

Cannot insert the value NULL into
column ‘CompanyID’, table
‘ConsumerCartel.dbo.CompanyOperatingState’;
column does not allow nulls. INSERT
fails. The statement has been
terminated.

However, if I make 2 changes it kind of works

Company company = _repo.GetSingle(123);
 // don't clear the list this time;
 foreach(string state in form["OperatingStates"].Split(',')) {
    OperatingState os = new OperatingState(state);
    // explicitly setting the company
    os.Company = company;
    company.OperatingStates.Add(os);
 }
 _repo.Save(company); // calls ISession.SaveOrUpdate

Which will add the new states in addition to the old ones, which is not what I want. However, even when explicitly setting the company (which I shouldn’t have to do when it’s added to a mapped list?) it doesn’t work if the list is cleared.

This code has worked on other entities, but not on this one, so I think this should work as written. What am I doing wrong?

  • 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-12T18:00:06+00:00Added an answer on May 12, 2026 at 6:00 pm

    Have you tried using Inverse()?

    HasMany(x => x.OperatingStates)
        .Inverse()
        .Cascade.All()
        .Table("OperatingState");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update : The problem appears to have been corrupt DLLs somewhere in my PHP
UPDATE 6/21/12 I have a form in rails that is working similar to an
I am using cakephp 1.2 and I have an array that appears to have
I have an application that I have written to view logs from custom applications
I have a Dictionary<int, int> and would like to update certain elements all at
Update: I reported this as a bug to Apple and they fixed it! All
Update : This is no longer an issue from C# 6, which has introduced
UPDATE UPI_ATTRIBUTE SET SITE_INC ='0' WHERE USER_PROFILING_NAME IN ('CAR_IMPLICIT','CAR_EXPLICIT') Above is my query that
I have a web page that contains a div element. On the page, there
Early warning - code sample a little long... I have a singleton NSMutableArray that

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.