Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 3e715f3

Browse files
authored
copy command timeout from the original dbcontext (#94)
1 parent 34b4690 commit 3e715f3

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsDataConnection.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public LinqToDBForEFToolsDataConnection(
5858
Context = context;
5959
_model = model;
6060
_transformFunc = transformFunc;
61+
CopyCommandTimeout();
6162
if (LinqToDBForEFTools.EnableChangeTracker)
6263
OnEntityCreated += OnEntityCreatedHandler;
6364
}
@@ -81,6 +82,7 @@ public LinqToDBForEFToolsDataConnection(
8182
Context = context;
8283
_model = model;
8384
_transformFunc = transformFunc;
85+
CopyCommandTimeout();
8486
if (LinqToDBForEFTools.EnableChangeTracker)
8587
OnEntityCreated += OnEntityCreatedHandler;
8688
}
@@ -103,6 +105,7 @@ public LinqToDBForEFToolsDataConnection(
103105
Context = context;
104106
_model = model;
105107
_transformFunc = transformFunc;
108+
CopyCommandTimeout();
106109
if (LinqToDBForEFTools.EnableChangeTracker)
107110
OnEntityCreated += OnEntityCreatedHandler;
108111
}
@@ -169,5 +172,10 @@ private void OnEntityCreatedHandler(EntityCreatedEventArgs args)
169172
args.Entity = entry.Entity;
170173
}
171174

175+
private void CopyCommandTimeout()
176+
{
177+
this.CommandTimeout =
178+
Context?.Database.GetCommandTimeout() ?? this.CommandTimeout;
179+
}
172180
}
173181
}

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/Models/Northwind/NorthwindContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System;
2+
using System.Reflection;
23
using LinqToDB.EntityFrameworkCore.BaseTests.Models.Northwind;
34
using LinqToDB.EntityFrameworkCore.SqlServer.Tests.Models.Northwind.Mapping;
45
using LinqToDB.Expressions;
@@ -28,6 +29,12 @@ public NorthwindContext(DbContextOptions options) : base(options)
2829

2930
}
3031

32+
[DbFunction("ProcessLong", "dbo")]
33+
public static int ProcessLong(int seconds)
34+
{
35+
throw new NotImplementedException();
36+
}
37+
3138
protected override void OnModelCreating(ModelBuilder builder)
3239
{
3340
builder.ApplyConfiguration(new CategoriesMap());

Tests/LinqToDB.EntityFrameworkCore.SqlServer.Tests/ToolsTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,5 +774,50 @@ public void TestNullability([Values(true, false)] bool enableFilter)
774774
}
775775

776776

777+
[Test]
778+
public void TestCommandTimeout()
779+
{
780+
int timeoutErrorCode = -2; // Timeout Expired
781+
int commandTimeout = 1;
782+
int commandExecutionTime = 5;
783+
var createProcessLongFunctionSql = // function that takes @secondsNumber seconds
784+
@"CREATE OR ALTER FUNCTION dbo.[ProcessLong]
785+
(
786+
@secondsNumber int
787+
)
788+
RETURNS int
789+
AS
790+
BEGIN
791+
declare @startTime datetime = getutcdate()
792+
while datediff(second, @startTime, getutcdate()) < @secondsNumber
793+
begin
794+
set @startTime = @startTime
795+
end
796+
return 1
797+
END";
798+
var dropProcessLongFunctionSql = @"DROP FUNCTION IF EXISTS [dbo].[ProcessLong]";
799+
800+
using (var ctx = CreateContext(false))
801+
{
802+
try
803+
{
804+
ctx.Database.ExecuteSqlRaw(createProcessLongFunctionSql);
805+
ctx.Database.SetCommandTimeout(commandTimeout);
806+
807+
var query = from p in ctx.Products
808+
select NorthwindContext.ProcessLong(commandExecutionTime);
809+
810+
var exception = Assert.Throws<Microsoft.Data.SqlClient.SqlException>(() =>
811+
{
812+
var result = query.ToLinqToDB().First();
813+
});
814+
Assert.AreEqual(exception.Number, timeoutErrorCode);
815+
}
816+
finally
817+
{
818+
ctx.Database.ExecuteSqlRaw(dropProcessLongFunctionSql);
819+
}
820+
}
821+
}
777822
}
778823
}

0 commit comments

Comments
 (0)