@@ -473,7 +473,8 @@ IList<Cat> oldCats =
473
473
<para >
474
474
Beginning with NHibernate 5.0, Linq queries can be used for inserting, updating or deleting entities.
475
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 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,
477
478
or instruct in which way it should be updated or inserted. Those queries happen entirely inside the
478
479
database, without extracting corresponding entities out of the database.
479
480
</para >
@@ -485,34 +486,34 @@ IList<Cat> oldCats =
485
486
<sect2 id =" querylinq-modifying-insert" >
486
487
<title >Inserting new entities</title >
487
488
<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.
492
493
</para >
493
494
<para >
494
495
Using projection to target entity:
495
496
</para >
496
497
<programlisting ><![CDATA[ session.Query<Cat>()
497
498
.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 >
500
500
<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:
502
503
</para >
503
504
<programlisting ><![CDATA[ session.Query<Cat>()
504
505
.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 >
507
507
<para >
508
508
Or using assignments:
509
509
</para >
510
510
<programlisting ><![CDATA[ session.Query<Cat>()
511
511
.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 >
516
517
<para >
517
518
In all cases, unspecified properties are not included in the resulting SQL insert.
518
519
<link linkend =" mapping-declaration-version" ><literal >version</literal ></link > and
@@ -528,32 +529,30 @@ IList<Cat> oldCats =
528
529
<sect2 id =" querylinq-modifying-update" >
529
530
<title >Updating entities</title >
530
531
<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.
534
535
</para >
535
536
<para >
536
537
Using projection to updated entity:
537
538
</para >
538
539
<programlisting ><![CDATA[ session.Query<Cat>()
539
540
.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 >
542
542
<para >
543
543
Projections can be done with an anonymous object too:
544
544
</para >
545
545
<programlisting ><![CDATA[ session.Query<Cat>()
546
546
.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 >
549
548
<para >
550
549
Or using assignments:
551
550
</para >
552
551
<programlisting ><![CDATA[ session.Query<Cat>()
553
552
.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 >
557
556
<para >
558
557
In all cases, unspecified properties are not included in the resulting SQL update. This could
559
558
be changed for <link linkend =" mapping-declaration-version" ><literal >version</literal ></link > and
@@ -562,6 +561,11 @@ IList<Cat> oldCats =
562
561
the version. Custom version types (<literal >NHibernate.Usertype.IUserVersionType</literal >) are
563
562
not supported.
564
563
</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 >
565
569
</sect2 >
566
570
567
571
<sect2 id =" querylinq-modifying-delete" >
0 commit comments