Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bulk Update using UpdateBuilder #3141

Answered by bahusoid
satishviswanathan asked this question in Q&A
Discussion options

I'm trying to do a bulk update as shown below.

 await _session.Query<Employee>()
 .Where(x => x.Name.StartsWith("sa"))
 .UpdateBuilder().Set(y => y.Score.Performance, 10)
 .UpdateAsync(ct).ConfigureAwait(false);

This is what I'm seeing in the error. Seems like the join between Employee and Score is not working.

NHibernate: update Employee set Score.Performance=:p0 where Name like (:p1||'%');:p0 = 10 [Type: Int32 (0:0:0)], :p1 = 'sa' [Type: String (0:0:0)]

Models

 public class Employee 
 {
 public Employee()
 {
 
 }
 public virtual string Name { get; set; }
 public virtual string Address { get; set; }
 public virtual Score Score { get; set; }
 }
 public class Score 
 {
 public virtual Employee EmployeeId { get; set; }
 public virtual int Performance { get; set; }
 }

Mapping

 public sealed class EmployeeMap
 {
 public EmployeeMap() : base("Employee", "Id")
 {
 Id(x => x.Id);
 Map(x => x.Name);
 Map(x => x.Address);
 HasOne(x => x.DeptId);
 HasOne(x => x.Score).PropertyRef(r=>r.EmployeeId).Fetch.Join().ForeignKey("EMPLOYEEID").Cascade.All();
 DynamicUpdate();
 DynamicInsert();
 Table("Employee");
 }
}
public class ScoreMap
{
 public ScoreMap() 
 {
 Id(x => x.Id);
 References(x => x.EmployeeId).Columns("EMPLOYEEID").Cascade.All();
 Map(x => x.Performance);
 DynamicUpdate();
 DynamicInsert();
 Table("Score");
 }
}

Not sure what i'm missing here.

You must be logged in to vote

Not sure what i'm missing here.

Updates on association entities (y.Score.Performance) are not supported.

As a workaround you can try to rewrite this DML using subquery. Something like:

IQueryable<Score> scoreToUpdateSubQuery = session.Query<Employee>().Where(x => x.Name.StartsWith("sa")).Select(e => e.Score);
await session.Query<Score>()
 .Where(e => scoreToUpdateSubQuery.Contains(e))
 .UpdateBuilder().Set(s => s.Performance, 10)
 .UpdateAsync(ct).ConfigureAwait(false);

Replies: 1 comment

Comment options

Not sure what i'm missing here.

Updates on association entities (y.Score.Performance) are not supported.

As a workaround you can try to rewrite this DML using subquery. Something like:

IQueryable<Score> scoreToUpdateSubQuery = session.Query<Employee>().Where(x => x.Name.StartsWith("sa")).Select(e => e.Score);
await session.Query<Score>()
 .Where(e => scoreToUpdateSubQuery.Contains(e))
 .UpdateBuilder().Set(s => s.Performance, 10)
 .UpdateAsync(ct).ConfigureAwait(false);
You must be logged in to vote
0 replies
Answer selected by hazzik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #3001 on August 29, 2022 21:51.

AltStyle によって変換されたページ (->オリジナル) /