Skip to content

Commit 18134d7

Browse files
NH-3944 - Updating the documentation for the new Linq DML syntax.
1 parent 02761b7 commit 18134d7

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

doc/reference/modules/query_linq.xml

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ IList<Cat> oldCats =
473473
<para>
474474
Beginning with NHibernate 5.0, Linq queries can be used for inserting, updating or deleting entities.
475475
The query defines the data to delete, update or insert, and then <literal>Delete</literal>,
476-
<literal>Update</literal> and <literal>Insert</literal> queryable extension methods allow to delete it,
476+
<literal>Update</literal>, <literal>UpdateBuilder</literal>, <literal>InsertInto</literal> and
477+
<literal>InsertBuilder</literal> queryable extension methods allow to delete it,
477478
or instruct in which way it should be updated or inserted. Those queries happen entirely inside the
478479
database, without extracting corresponding entities out of the database.
479480
</para>
@@ -485,34 +486,34 @@ IList<Cat> oldCats =
485486
<sect2 id="querylinq-modifying-insert">
486487
<title>Inserting new entities</title>
487488
<para>
488-
<literal>Insert</literal> method extension expects a NHibernate queryable defining the data source of
489-
the insert. This data can be entities or a projection. Then it allows specifying the target entity type
490-
to insert, and how to convert source data to those target entities. Three forms of target specification
491-
exist.
489+
<literal>InsertInto</literal> and <literal>InsertBuilder</literal> method extensions expect a NHibernate
490+
queryable defining the data source of the insert. This data can be entities or a projection. Then they
491+
allow specifying the target entity type to insert, and how to convert source data to those target
492+
entities. Three forms of target specification exist.
492493
</para>
493494
<para>
494495
Using projection to target entity:
495496
</para>
496497
<programlisting><![CDATA[session.Query<Cat>()
497498
.Where(c => c.BodyWeight > 20)
498-
.Insert()
499-
.As(c => new Dog { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]></programlisting>
499+
.InsertInto(c => new Dog { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]></programlisting>
500500
<para>
501-
Projections can be done with an anonymous object too, but it requires supplying explicitly the target type:
501+
Projections can be done with an anonymous object too, but it requires supplying explicitly the target
502+
type, which in turn requires re-specifying the source type:
502503
</para>
503504
<programlisting><![CDATA[session.Query<Cat>()
504505
.Where(c => c.BodyWeight > 20)
505-
.Insert()
506-
.As<Dog>(c => new { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]></programlisting>
506+
.InsertInto<Cat, Dog>(c => new { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]></programlisting>
507507
<para>
508508
Or using assignments:
509509
</para>
510510
<programlisting><![CDATA[session.Query<Cat>()
511511
.Where(c => c.BodyWeight > 20)
512-
.Insert()
513-
.Into<Dog>(a => a
514-
.Set(d => d.Name, c => c.Name + "dog")
515-
.Set(d => d.BodyWeight, c => c.BodyWeight));]]></programlisting>
512+
.InsertBuilder()
513+
.Into<Dog>()
514+
.Value(d => d.Name, c => c.Name + "dog")
515+
.Value(d => d.BodyWeight, c => c.BodyWeight)
516+
.Insert();]]></programlisting>
516517
<para>
517518
In all cases, unspecified properties are not included in the resulting SQL insert.
518519
<link linkend="mapping-declaration-version"><literal>version</literal></link> and
@@ -528,32 +529,30 @@ IList<Cat> oldCats =
528529
<sect2 id="querylinq-modifying-update">
529530
<title>Updating entities</title>
530531
<para>
531-
<literal>Update</literal> method extension expects a queryable defining the entities to update.
532-
Then it allows specifying which properties should be updated with which values. As for
533-
<literal>Insert</literal>, three forms of target specification exist.
532+
<literal>Update</literal> and <literal>UpdateBuilder</literal> method extensions expect a NHibernate
533+
queryable defining the entities to update. Then they allow specifying which properties should be
534+
updated with which values. As for insertion, three forms of target specification exist.
534535
</para>
535536
<para>
536537
Using projection to updated entity:
537538
</para>
538539
<programlisting><![CDATA[session.Query<Cat>()
539540
.Where(c => c.BodyWeight > 20)
540-
.Update()
541-
.As(c => new Cat { BodyWeight = c.BodyWeight / 2 });]]></programlisting>
541+
.Update(c => new Cat { BodyWeight = c.BodyWeight / 2 });]]></programlisting>
542542
<para>
543543
Projections can be done with an anonymous object too:
544544
</para>
545545
<programlisting><![CDATA[session.Query<Cat>()
546546
.Where(c => c.BodyWeight > 20)
547-
.Update()
548-
.As(c => new { BodyWeight = c.BodyWeight / 2 });]]></programlisting>
547+
.Update(c => new { BodyWeight = c.BodyWeight / 2 });]]></programlisting>
549548
<para>
550549
Or using assignments:
551550
</para>
552551
<programlisting><![CDATA[session.Query<Cat>()
553552
.Where(c => c.BodyWeight > 20)
554-
.Update()
555-
.Assign(a => a
556-
.Set(c => c.BodyWeight, c => c.BodyWeight / 2));]]></programlisting>
553+
.UpdateBuilder()
554+
.Set(c => c.BodyWeight, c => c.BodyWeight / 2)
555+
.Update();]]></programlisting>
557556
<para>
558557
In all cases, unspecified properties are not included in the resulting SQL update. This could
559558
be changed for <link linkend="mapping-declaration-version"><literal>version</literal></link> and
@@ -562,6 +561,11 @@ IList<Cat> oldCats =
562561
the version. Custom version types (<literal>NHibernate.Usertype.IUserVersionType</literal>) are
563562
not supported.
564563
</para>
564+
<para>
565+
When using projection to updated entity, please note that the constructed entity must have the
566+
exact same type than the underlying queryable source type. Attempting to project to any other class
567+
(anonymous projections excepted) will fail.
568+
</para>
565569
</sect2>
566570

567571
<sect2 id="querylinq-modifying-delete">

0 commit comments

Comments
 (0)