Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate configuration for uni-directional one-to-many relation
    text
    copied!<p>I'm trying to set up a relationship as follows. Each <strong>Master</strong> item has one or more <strong>Detail</strong> items:</p> <pre><code>public class Detail { public virtual Guid DetailId { get; set; } public virtual string Name { get; set; } } public class Master { public virtual Guid MasterId { get; set; } public virtual string Name { get; set; } public virtual IList&lt;Detail&gt; Details { get; set; } } </code></pre> <p>And Mappings:</p> <pre><code>public class MasterMap : ClassMap&lt;Master&gt; { public MasterMap() { Id(x =&gt; x.MasterId); Map(x =&gt; x.Name); HasMany(x =&gt; x.Details).Not.KeyNullable.Cascade.All(); } } public class DetailMap : ClassMap&lt;Detail&gt; { public DetailMap() { Id(x =&gt; x.Id); Map(x =&gt; x.Name); } } </code></pre> <p>The <strong>Master</strong> database table is:</p> <pre><code>masterId uniqueidentifier NOT NULL name nvarchar(max) NULL </code></pre> <p>and <strong>Detail</strong> is:</p> <pre><code>DetailId uniqueidentifier NOT NULL name nvarchar(max) NULL MasterId uniqueidentifier NULL foreign key (masterId) references [Master] </code></pre> <p>I don't really care to have a link from Detail back to Master -- in otherwords, Detail objects on their own are just not interesting to my domain layer. They will <em>always</em> be accessed via their Master object.</p> <p>Using code like this:</p> <pre><code>Master mast = new Master { MasterId = new Guid(), Name = "test", Details = new List&lt;Detail&gt; { new Detail { .DetailId = new Guid(), .Name = "Test1" }, new Detail { .DetailId = new Guid(), .Name = "Test1" } } }; using (transaction == Session.BeginTransaction) { Session.Save(mast); transaction.Commit(); } </code></pre> <p>This works great, except for a crazy limitation outlined in <a href="https://stackoverflow.com/questions/554674/fluent-nhibernate-one-to-many-uni-directional-mapping">this post</a>: NHibernate does an INSERT and puts Detail.MasterId as NULL first, then does an UPDATE to set it to the real MasterId.</p> <p>Really, I don't want Detail entries with NULL MasterIds, so if I set the MasterId field to NOT NULL, the INSERT to Detail will fail, because as I said NHibernate is trying to put in MasterId = NULL. </p> <p>I guess my question boils down to this:</p> <p>How can I get the above code sample to work with my existing domain model (eg, without adding a Detail.Master property), and the Detail.MasterId field in the database set to NOT NULL?</p> <p>Is there a way to get Nhibernate to just put the correct MasterId in the initial INSERT, rather than running an UPDATE afterwards? Is there rationale somewhere for this design decision? -- I'm struggling to see why it would be done this way.</p>
 

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