@@ -467,6 +467,84 @@ IList<Cat> oldCats =
467467 .ToList();]]> </programlisting >
468468 </sect1 >
469469
470+ <sect1 id =" querylinq-modifying" >
471+ <title >Modifying entities inside the database</title >
472+
473+ <para >
474+ Beginning with NHibernate 5, Linq queries can be used for inserting, updating or deleting entities.
475+ 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
477+ to delete it, or instruct in which way it should updated or inserted. Those queries then happen
478+ entirely inside the database, without extracting corresponding entities out of the database.
479+ </para >
480+
481+ <sect2 id =" querylinq-modifying-insert" >
482+ <title >Inserting new entities</title >
483+ <para >
484+ <literal >Insert</literal > method extension expects a queryable defining the data source of the insert.
485+ This data can be entities or a projection. Then it allows specifying the target entity type to insert,
486+ and how to convert source data to those target entities. Two forms of target specification exist.
487+ </para >
488+ <para >
489+ Using projection to target entity:
490+ </para >
491+ <programlisting ><![CDATA[ session.Query<Cat>()
492+ .Where(c => c.BodyWeight > 20)
493+ .Insert()
494+ .As(c => new Dog { Name = c.Name + "dog", BodyWeight = c.BodyWeight });]]> </programlisting >
495+ <para >
496+ Or using assignments:
497+ </para >
498+ <programlisting ><![CDATA[ session.Query<Cat>()
499+ .Where(c => c.BodyWeight > 20)
500+ .Insert()
501+ .Into<Dog>(a => a
502+ .Set(d => d.Name, c => c.Name + "dog")
503+ .Set(d => d.BodyWeight, c => c.BodyWeight));]]> </programlisting >
504+ <para >
505+ In both cases, unspecified properties are not included in the resulting SQL insert.
506+ </para >
507+ </sect2 >
508+
509+ <sect2 id =" querylinq-modifying-update" >
510+ <title >Updating entities</title >
511+ <para >
512+ <literal >Update</literal > method extension expects a queryable defining the entities to update.
513+ Then it allows specifying which properties should be updated with which values. As for
514+ <literal >Insert</literal >, two forms of target specification exist.
515+ </para >
516+ <para >
517+ Using projection to updated entity:
518+ </para >
519+ <programlisting ><![CDATA[ session.Query<Cat>()
520+ .Where(c => c.BodyWeight > 20)
521+ .Update()
522+ .As(c => new Cat { BodyWeight = c.BodyWeight / 2 });]]> </programlisting >
523+ <para >
524+ Or using assignments:
525+ </para >
526+ <programlisting ><![CDATA[ session.Query<Cat>()
527+ .Where(c => c.BodyWeight > 20)
528+ .Update()
529+ .Assign(a => a
530+ .Set(c => c.BodyWeight, c => c.BodyWeight / 2));]]> </programlisting >
531+ <para >
532+ In both cases, unspecified properties are not included in the resulting SQL update.
533+ </para >
534+ </sect2 >
535+
536+ <sect2 id =" querylinq-modifying-delete" >
537+ <title >Deleting entities</title >
538+ <para >
539+ <literal >Delete</literal > method extension expects a queryable defining the entities to delete.
540+ It immediately deletes them.
541+ </para >
542+ <programlisting ><![CDATA[ session.Query<Cat>()
543+ .Where(c => c.BodyWeight > 20)
544+ .Delete();]]> </programlisting >
545+ </sect2 >
546+ </sect1 >
547+
470548 <sect1 id =" querylinq-querycache" >
471549 <title >Query cache</title >
472550
0 commit comments