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

Commit e78cda4

Browse files
MaceWindusdanyliv
andauthored
NET5.0 support (#72)
* .net 5.0 build/nuget support * remove 3.1 upport * Corrected tests. * Migrated to EF Core 5. * Fixed tracing and merged with version 3. * Corrected versions. * fix condition * install .net 5 on azure * temporary use net3.1 for tests Co-authored-by: Svyatoslav Danyliv <sdanyliv@gmail.com>
1 parent f6581ab commit e78cda4

21 files changed

+289
-143
lines changed

Build/linq2db.Default.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>3.7.0</Version>
3+
<Version>5.0.0</Version>
44

55
<Description>Allows to execute Linq to DB (linq2db) queries in Entity Framework Core DbContext.</Description>
66
<Title>Linq to DB (linq2db) extensions for Entity Framework Core</Title>

Build/linq2db.Tests.props

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project>
2+
<PropertyGroup>
3+
<!--<TargetFrameworks>net5.0</TargetFrameworks>-->
4+
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
9+
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
</PackageReference>
13+
<PackageReference Include="NUnit" Version="3.12.0" />
14+
</ItemGroup>
15+
16+
</Project>

NuGet/linq2db.EntityFrameworkCore.nuspec

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
<projectUrl>https://github.com/linq2db/linq2db.EntityFrameworkCore</projectUrl>
1515
<license type="file">MIT-LICENSE.txt</license>
1616
<dependencies>
17-
<group targetFramework=".NETStandard2.0">
18-
<dependency id="Microsoft.EntityFrameworkCore.Relational" version="3.1.3" />
19-
<dependency id="linq2db" version="3.1.5" />
17+
<group targetFramework=".NETStandard2.1">
18+
<dependency id="Microsoft.EntityFrameworkCore.Relational" version="5.0.0" />
19+
<dependency id="linq2db" version="3.1.6" />
2020
</group>
2121
</dependencies>
2222
</metadata>
2323

2424
<files>
25-
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\**\linq2db.EntityFrameworkCore.pdb" target="lib\" />
26-
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\**\linq2db.EntityFrameworkCore.xml" target="lib\" />
27-
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\**\linq2db.EntityFrameworkCore.dll" target="lib\" />
25+
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\netstandard2.1\linq2db.EntityFrameworkCore.pdb" target="lib\" />
26+
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\netstandard2.1\linq2db.EntityFrameworkCore.xml" target="lib\" />
27+
<file src="..\Source\LinqToDB.EntityFrameworkCore\bin\Release\netstandard2.1\linq2db.EntityFrameworkCore.dll" target="lib\" />
2828

2929
<file src="..\MIT-LICENSE.txt" />
3030

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

+34-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using LinqToDB.Expressions;
88
using LinqToDB.Reflection;
99
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.EntityFrameworkCore.Diagnostics;
1011
using Microsoft.EntityFrameworkCore.Metadata;
1112
using Microsoft.EntityFrameworkCore.Metadata.Internal;
1213
using Microsoft.EntityFrameworkCore.Query;
@@ -32,12 +33,18 @@ internal class EFCoreMetadataReader : IMetadataReader
3233
private readonly RelationalSqlTranslatingExpressionVisitorDependencies _dependencies;
3334
private readonly IRelationalTypeMappingSource _mappingSource;
3435
private readonly ConcurrentDictionary<MemberInfo, EFCoreExpressionAttribute> _calculatedExtensions = new ConcurrentDictionary<MemberInfo, EFCoreExpressionAttribute>();
36+
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _logger;
3537

36-
public EFCoreMetadataReader(IModel model, RelationalSqlTranslatingExpressionVisitorDependencies dependencies, IRelationalTypeMappingSource mappingSource)
38+
public EFCoreMetadataReader(IModel model,
39+
RelationalSqlTranslatingExpressionVisitorDependencies dependencies,
40+
IRelationalTypeMappingSource mappingSource,
41+
IDiagnosticsLogger<DbLoggerCategory.Query> logger
42+
)
3743
{
3844
_model = model;
3945
_dependencies = dependencies;
4046
_mappingSource = mappingSource;
47+
_logger = logger;
4148
}
4249

4350
public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute
@@ -189,16 +196,30 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
189196
foreach (var navigation in navigations)
190197
{
191198
var fk = navigation.ForeignKey;
192-
193-
var thisKey = string.Join(",", fk.Properties.Select(p => p.Name));
194-
var otherKey = string.Join(",", fk.PrincipalKey.Properties.Select(p => p.Name));
195-
associations.Add(new AssociationAttribute
199+
if (!navigation.IsOnDependent)
196200
{
197-
ThisKey = thisKey,
198-
OtherKey = otherKey,
199-
CanBeNull = !fk.IsRequired,
200-
IsBackReference = fk.PrincipalEntityType != et
201-
});
201+
var thisKey = string.Join(",", fk.PrincipalKey.Properties.Select(p => p.Name));
202+
var otherKey = string.Join(",", fk.Properties.Select(p => p.Name));
203+
associations.Add(new AssociationAttribute
204+
{
205+
ThisKey = thisKey,
206+
OtherKey = otherKey,
207+
CanBeNull = !fk.IsRequired,
208+
IsBackReference = false
209+
});
210+
}
211+
else
212+
{
213+
var thisKey = string.Join(",", fk.Properties.Select(p => p.Name));
214+
var otherKey = string.Join(",", fk.PrincipalKey.Properties.Select(p => p.Name));
215+
associations.Add(new AssociationAttribute
216+
{
217+
ThisKey = thisKey,
218+
OtherKey = otherKey,
219+
CanBeNull = !fk.IsRequired,
220+
IsBackReference = true
221+
});
222+
}
202223
}
203224

204225
return associations.Select(a => (T)(Attribute)a).ToArray();
@@ -298,7 +319,7 @@ public SqlTransparentExpression(Expression expression, RelationalTypeMapping typ
298319
Expression = expression;
299320
}
300321

301-
public override void Print(ExpressionPrinter expressionPrinter)
322+
protected override void Print(ExpressionPrinter expressionPrinter)
302323
{
303324
expressionPrinter.Print(Expression);
304325
}
@@ -327,7 +348,7 @@ private Sql.ExpressionAttribute GetDbFunctionFromMethodCall(Type type, MethodInf
327348
Expression.Constant(DefaultValue.GetValue(p.ParameterType), p.ParameterType),
328349
_mappingSource?.FindMapping(p.ParameterType))).ToArray();
329350

330-
var newExpression = _dependencies.MethodCallTranslatorProvider.Translate(_model, objExpr, methodInfo, parametersArray);
351+
var newExpression = _dependencies.MethodCallTranslatorProvider.Translate(_model, objExpr, methodInfo, parametersArray, _logger);
331352
if (newExpression != null)
332353
{
333354
if (!methodInfo.IsStatic)
@@ -360,7 +381,7 @@ private Sql.ExpressionAttribute GetDbFunctionFromProperty(Type type, PropertyInf
360381
{
361382
var objExpr = new SqlTransparentExpression(Expression.Constant(DefaultValue.GetValue(type), type), _mappingSource?.FindMapping(propInfo));
362383

363-
var newExpression = _dependencies.MemberTranslatorProvider.Translate(objExpr, propInfo, propInfo.GetMemberType());
384+
var newExpression = _dependencies.MemberTranslatorProvider.Translate(objExpr, propInfo, propInfo.GetMemberType(), _logger);
364385
if (newExpression != null)
365386
{
366387
var parametersArray = new Expression[] { objExpr };

Source/LinqToDB.EntityFrameworkCore/ILinqToDBForEFTools.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq.Expressions;
33

44
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Diagnostics;
56
using Microsoft.EntityFrameworkCore.Infrastructure;
67
using Microsoft.EntityFrameworkCore.Metadata;
78
using Microsoft.EntityFrameworkCore.Query;
@@ -40,8 +41,11 @@ public interface ILinqToDBForEFTools
4041
/// <param name="model">EF.Core data model.</param>
4142
/// <param name="dependencies"></param>
4243
/// <param name="mappingSource"></param>
44+
/// <param name="logger"></param>
4345
/// <returns>LINQ To DB metadata provider for specified EF.Core model. Can return <c>null</c>.</returns>
44-
IMetadataReader CreateMetadataReader(IModel model, RelationalSqlTranslatingExpressionVisitorDependencies dependencies, IRelationalTypeMappingSource mappingSource);
46+
IMetadataReader CreateMetadataReader(IModel model, RelationalSqlTranslatingExpressionVisitorDependencies dependencies,
47+
IRelationalTypeMappingSource mappingSource,
48+
IDiagnosticsLogger<DbLoggerCategory.Query> logger);
4549

4650
/// <summary>
4751
/// Creates mapping schema using provided EF.Core data model and metadata provider.

Source/LinqToDB.EntityFrameworkCore/Internal/LinqToDBForEFQueryProvider.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99

10+
using Microsoft.EntityFrameworkCore.Query;
1011
using Microsoft.EntityFrameworkCore.Query.Internal;
1112

1213
using JetBrains.Annotations;

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFTools.cs

+16-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Extensions.Logging;
1313

1414
using JetBrains.Annotations;
15+
using Microsoft.EntityFrameworkCore.Diagnostics;
1516
using Microsoft.EntityFrameworkCore.Query;
1617
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
1718

@@ -94,7 +95,7 @@ public static ILinqToDBForEFTools Implementation
9495
{
9596
_implementation = value ?? throw new ArgumentNullException(nameof(value));
9697
_metadataReaders.Clear();
97-
_defaultMetadataReader = new Lazy<IMetadataReader>(() => Implementation.CreateMetadataReader(null, null, null));
98+
_defaultMetadataReader = new Lazy<IMetadataReader>(() => Implementation.CreateMetadataReader(null, null, null, null));
9899
}
99100
}
100101

@@ -125,14 +126,15 @@ static LinqToDBForEFTools()
125126
/// <param name="model">EF.Core data model instance. Could be <c>null</c>.</param>
126127
/// <param name="dependencies"></param>
127128
/// <param name="mappingSource"></param>
129+
/// <param name="logger"></param>
128130
/// <returns>LINQ To DB metadata provider.</returns>
129131
public static IMetadataReader GetMetadataReader([JetBrains.Annotations.CanBeNull] IModel model,
130-
RelationalSqlTranslatingExpressionVisitorDependencies dependencies, IRelationalTypeMappingSource mappingSource)
132+
RelationalSqlTranslatingExpressionVisitorDependencies dependencies, IRelationalTypeMappingSource mappingSource, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
131133
{
132134
if (model == null)
133135
return _defaultMetadataReader.Value;
134136

135-
return _metadataReaders.GetOrAdd(model, m => Implementation.CreateMetadataReader(model, dependencies, mappingSource));
137+
return _metadataReaders.GetOrAdd(model, m => Implementation.CreateMetadataReader(model, dependencies, mappingSource, logger));
136138
}
137139

138140
/// <summary>
@@ -219,13 +221,15 @@ public static IDataProvider GetDataProvider(EFProviderInfo info, EFConnectionInf
219221
/// <param name="convertorSelector">EF Core registry for type conversion.</param>
220222
/// <param name="dependencies"></param>
221223
/// <param name="mappingSource"></param>
224+
/// <param name="logger"></param>
222225
/// <returns>Mapping schema for provided EF.Core model.</returns>
223226
public static MappingSchema GetMappingSchema(IModel model,
224227
IValueConverterSelector convertorSelector,
225228
RelationalSqlTranslatingExpressionVisitorDependencies dependencies,
226-
IRelationalTypeMappingSource mappingSource)
229+
IRelationalTypeMappingSource mappingSource,
230+
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
227231
{
228-
return Implementation.GetMappingSchema(model, GetMetadataReader(model, dependencies, mappingSource), convertorSelector);
232+
return Implementation.GetMappingSchema(model, GetMetadataReader(model, dependencies, mappingSource, logger), convertorSelector);
229233
}
230234

231235
/// <summary>
@@ -292,7 +296,8 @@ public static DataConnection CreateLinqToDbConnection(this DbContext context,
292296
var dependencies = context.GetService<RelationalSqlTranslatingExpressionVisitorDependencies>();
293297
var mappingSource = context.GetService<IRelationalTypeMappingSource>();
294298
var converters = context.GetService<IValueConverterSelector>();
295-
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource);
299+
var dLogger = context.GetService<IDiagnosticsLogger<DbLoggerCategory.Query>>();
300+
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource, dLogger);
296301
if (mappingSchema != null)
297302
dc.AddMappingSchema(mappingSchema);
298303

@@ -329,7 +334,8 @@ public static IDataContext CreateLinqToDbContext(this DbContext context,
329334
var dependencies = context.GetService<RelationalSqlTranslatingExpressionVisitorDependencies>();
330335
var mappingSource = context.GetService<IRelationalTypeMappingSource>();
331336
var converters = context.GetService<IValueConverterSelector>();
332-
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource);
337+
var dLogger = context.GetService<IDiagnosticsLogger<DbLoggerCategory.Query>>();
338+
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource, dLogger);
333339
var logger = CreateLogger(info.Options);
334340

335341
if (transaction != null)
@@ -400,7 +406,8 @@ public static DataConnection CreateLinq2DbConnectionDetached([JetBrains.Annotati
400406
var dependencies = context.GetService<RelationalSqlTranslatingExpressionVisitorDependencies>();
401407
var mappingSource = context.GetService<IRelationalTypeMappingSource>();
402408
var converters = context.GetService<IValueConverterSelector>();
403-
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource);
409+
var dLogger = context.GetService<IDiagnosticsLogger<DbLoggerCategory.Query>>();
410+
var mappingSchema = GetMappingSchema(context.Model, converters, dependencies, mappingSource, dLogger);
404411
if (mappingSchema != null)
405412
dc.AddMappingSchema(mappingSchema);
406413

@@ -495,7 +502,7 @@ public static DataConnection CreateLinqToDbConnection(this DbContextOptions opti
495502

496503
if (model != null)
497504
{
498-
var mappingSchema = GetMappingSchema(model, null, null, null);
505+
var mappingSchema = GetMappingSchema(model, null, null, null, null);
499506
if (mappingSchema != null)
500507
dc.AddMappingSchema(mappingSchema);
501508
}

0 commit comments

Comments
 (0)