Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate cascade delete not working
    primarykey
    data
    text
    <p>I've got a simple phone directory app using Fluent NHibernate 1.1. In the app, a "Person" object has many "PhoneNumber" objects. I'm trying to delete a Person and I want to cascade deletes to PhoneNumbers. I set a convention of <code>DefaultCascade.All()</code> after reading <a href="https://stackoverflow.com/questions/2242902/cascade-saves-with-fluent-nhibernate-automapping-old-anwser-still-valid">this answer</a>. However, attempting to delete the parent object still throws an exception--it appears that NHibernate is trying to update the child table to set the parent ID to null instead of just deleting the record:</p> <blockquote> <p>{"could not delete collection: [Person.PhoneNumbers#473][SQL: UPDATE phone_numbers SET person_id = null WHERE person_id = @p0]"}</p> </blockquote> <p>InnerException:</p> <blockquote> <p>{"Cannot insert the value NULL into column 'person_id', table 'directory.dbo.phone_numbers'; column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated."}</p> </blockquote> <p>My Fluent config is:</p> <pre><code>public static ISessionFactory CreateSessionFactory() { return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["activeConnStr"]].ConnectionString)) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;Person&gt;() .Conventions.Add(DefaultCascade.All()) ) .BuildSessionFactory(); } </code></pre> <p>The parent class is:</p> <pre><code>public class Person { public Person() { PhoneNumbers = new List&lt;PhoneNumber&gt;(); EmailAddresses = new List&lt;string&gt;(); } public virtual int Id { get; private set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual string Company { get; set; } public virtual IList&lt;PhoneNumber&gt; PhoneNumbers { get; set; } public virtual IList&lt;string&gt; EmailAddresses { get; set; } } </code></pre> <p>The child class (PhoneNumber) is:</p> <pre><code>public class PhoneNumber { public virtual string Number { get; set; } public virtual PhoneNumberType NumberType { get; set; } public virtual Person Person { get; set; } } </code></pre> <p>My code to delete a person is:</p> <pre><code>public static void DeletePerson(int id) { using (var session = Dalc.Instance.SessionFactory.OpenSession()) { using (var trans = session.BeginTransaction()) { session.Delete(session.Load&lt;Person&gt;(id)); trans.Commit(); } } } </code></pre> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload