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

Commit cf1bbf4

Browse files
committed
Added informative message for preventing usage of non relational providers.
1 parent c635574 commit cf1bbf4

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,15 @@ public virtual DbContext GetCurrentContext(IQueryable query)
728728
var queryContextFactoryField = compiler.GetType().GetField("_queryContextFactory", BindingFlags.NonPublic | BindingFlags.Instance);
729729

730730
if (queryContextFactoryField == null)
731-
throw new LinqToDBForEFToolsException($"Can not find private field '{compiler.GetType()}._queryContextFactory' in current EFCore Version");
731+
throw new LinqToDBForEFToolsException($"Can not find private field '{compiler.GetType()}._queryContextFactory' in current EFCore Version.");
732+
733+
if (!(queryContextFactoryField.GetValue(compiler) is RelationalQueryContextFactory queryContextFactory))
734+
throw new LinqToDBForEFToolsException("LinqToDB Tools for EFCore support only Relational Databases.");
732735

733-
var queryContextFactory = (RelationalQueryContextFactory) queryContextFactoryField.GetValue(compiler);
734736
var dependenciesProperty = typeof(RelationalQueryContextFactory).GetProperty("Dependencies", BindingFlags.NonPublic | BindingFlags.Instance);
735737

736738
if (queryContextFactoryField == null)
737-
throw new LinqToDBForEFToolsException($"Can not find private property '{nameof(RelationalQueryContextFactory)}.Dependencies' in current EFCore Version");
739+
throw new LinqToDBForEFToolsException($"Can not find private property '{nameof(RelationalQueryContextFactory)}.Dependencies' in current EFCore Version.");
738740

739741
var dependencies = (QueryContextDependencies) dependenciesProperty.GetValue(queryContextFactory);
740742

Tests/LinqToDB.EntityFrameworkCore.Tests/LinqToDB.EntityFrameworkCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.0.20171219.105559" />
99
<PackageReference Include="linq2db.MySql" Version="2.0.0-beta5" />
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" />
1011
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0-rc1-final" />
1112
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.0-rc1-final" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />

Tests/LinqToDB.EntityFrameworkCore.Tests/ToolsTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace LinqToDB.EntityFrameworkCore.Tests
1818
public class ToolsTests : TestsBase
1919
{
2020
private readonly DbContextOptions _options;
21+
private DbContextOptions<AdventureWorksContext> _inmemoryOptions;
2122

2223
static ToolsTests()
2324
{
@@ -34,6 +35,21 @@ public ToolsTests()
3435
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);
3536

3637
_options = optionsBuilder.Options;
38+
39+
optionsBuilder = new DbContextOptionsBuilder<AdventureWorksContext>();
40+
//new SqlServerDbContextOptionsBuilder(optionsBuilder);
41+
42+
optionsBuilder.UseInMemoryDatabase();
43+
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);
44+
45+
_inmemoryOptions = optionsBuilder.Options;
46+
}
47+
48+
private AdventureWorksContext CreateAdventureWorksContextInMemory()
49+
{
50+
var ctx = new AdventureWorksContext(_inmemoryOptions);
51+
ctx.Database.EnsureCreated();
52+
return ctx;
3753
}
3854

3955
private AdventureWorksContext CreateAdventureWorksContext()
@@ -493,6 +509,42 @@ public async Task TestIncludeMany()
493509

494510
}
495511

512+
[Test]
513+
public async Task TestInMemory()
514+
{
515+
using (var ctx = CreateAdventureWorksContextInMemory())
516+
{
517+
Assert.Throws<LinqToDBForEFToolsException>(() =>
518+
{
519+
ctx.SalesOrders.ToLinqToDB().ToArray();
520+
});
521+
522+
Assert.Throws<LinqToDBForEFToolsException>(() =>
523+
{
524+
var query = ctx.SalesOrders
525+
.Where(so => so.CustomerID == -1)
526+
.Delete();
527+
});
528+
}
529+
}
530+
531+
[Test]
532+
public async Task TestContinuousQueries()
533+
{
534+
Common.Configuration.Linq.AllowMultipleQuery = true;
535+
536+
using (var ctx = CreateAdventureWorksContext())
537+
{
538+
var query = ctx.SalesOrders
539+
.Include(o => o.Details)
540+
.ThenInclude(d => d.SalesOrder);
541+
542+
var expected = await query.ToLinqToDB().ToArrayAsync();
543+
var result = await query.ToLinqToDB().ToArrayAsync();
544+
}
545+
546+
}
547+
496548

497549
}
498550
}

0 commit comments

Comments
 (0)