Skip to content

Commit 55cd670

Browse files
bahusoidfredericDelaporte
authored andcommitted
Throw for LINQ DML on filter
1 parent 9ac4809 commit 55cd670

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

src/NHibernate.Test/Async/LinqBulkManipulation/Fixture.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,18 @@ public async Task DeleteSyntaxWithCompositeIdAsync()
11581158
}
11591159
}
11601160

1161+
[Test]
1162+
public async Task DeleteOnFilterThrowsAsync()
1163+
{
1164+
using (var s = OpenSession())
1165+
using (s.BeginTransaction())
1166+
{
1167+
var a = await (s.Query<SimpleEntityWithAssociation>().Take(1).SingleOrDefaultAsync());
1168+
var query = a.AssociatedEntities.AsQueryable();
1169+
Assert.That(() => query.Delete(), Throws.InstanceOf<NotSupportedException>());
1170+
}
1171+
}
1172+
11611173
#endregion
11621174
}
1163-
}
1175+
}

src/NHibernate.Test/LinqBulkManipulation/Fixture.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,18 @@ public void DeleteOnProjectionThrows()
11991199
}
12001200
}
12011201

1202+
[Test]
1203+
public void DeleteOnFilterThrows()
1204+
{
1205+
using (var s = OpenSession())
1206+
using (s.BeginTransaction())
1207+
{
1208+
var a = s.Query<SimpleEntityWithAssociation>().Take(1).SingleOrDefault();
1209+
var query = a.AssociatedEntities.AsQueryable();
1210+
Assert.That(() => query.Delete(), Throws.InstanceOf<NotSupportedException>());
1211+
}
1212+
}
1213+
12021214
#endregion
12031215
}
1204-
}
1216+
}

src/NHibernate/Async/Linq/DefaultQueryProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ protected virtual Task<object> ExecuteQueryAsync(NhLinqExpression nhLinqExpressi
7272

7373
public Task<int> ExecuteDmlAsync<T>(QueryMode queryMode, Expression expression, CancellationToken cancellationToken)
7474
{
75+
if (Collection != null)
76+
throw new NotSupportedException("DML operations are not supported for filters.");
7577
if (cancellationToken.IsCancellationRequested)
7678
{
7779
return Task.FromCanceled<int>(cancellationToken);
7880
}
7981
try
8082
{
83+
8184
var nhLinqExpression = new NhLinqDmlExpression<T>(queryMode, expression, Session.Factory);
8285

8386
var query = Session.CreateQuery(nhLinqExpression);

src/NHibernate/Linq/DefaultQueryProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ public virtual void SetResultTransformerAndAdditionalCriteria(IQuery query, NhLi
265265

266266
public int ExecuteDml<T>(QueryMode queryMode, Expression expression)
267267
{
268+
if (Collection != null)
269+
throw new NotSupportedException("DML operations are not supported for filters.");
270+
268271
var nhLinqExpression = new NhLinqDmlExpression<T>(queryMode, expression, Session.Factory);
269272

270273
var query = Session.CreateQuery(nhLinqExpression);

0 commit comments

Comments
 (0)