Skip to content

Commit 4b51721

Browse files
bahusoidfredericDelaporte
authored andcommitted
Throw for LINQ DML on filter
1 parent cef5184 commit 4b51721

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

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

+13-1
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

+13-1
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

+3
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ protected virtual async Task<object> ExecuteQueryAsync(NhLinqExpression nhLinqEx
5858

5959
public Task<int> ExecuteDmlAsync<T>(QueryMode queryMode, Expression expression, CancellationToken cancellationToken)
6060
{
61+
if (Collection != null)
62+
throw new NotSupportedException("DML operations are not supported for filters.");
6163
if (cancellationToken.IsCancellationRequested)
6264
{
6365
return Task.FromCanceled<int>(cancellationToken);
6466
}
6567
try
6668
{
69+
6770
var nhLinqExpression = new NhLinqDmlExpression<T>(queryMode, expression, Session.Factory);
6871

6972
var query = Session.CreateQuery(nhLinqExpression);

src/NHibernate/Linq/DefaultQueryProvider.cs

+3
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ public virtual void SetResultTransformerAndAdditionalCriteria(IQuery query, NhLi
220220

221221
public int ExecuteDml<T>(QueryMode queryMode, Expression expression)
222222
{
223+
if (Collection != null)
224+
throw new NotSupportedException("DML operations are not supported for filters.");
225+
223226
var nhLinqExpression = new NhLinqDmlExpression<T>(queryMode, expression, Session.Factory);
224227

225228
var query = Session.CreateQuery(nhLinqExpression);

0 commit comments

Comments
 (0)