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

Commit 9461490

Browse files
authored
FSharp issue with EFCoreExpressionAttribute or UseFSharpTypes (#246)
* add test for linq2db/linq2db#3743 * update test * proposed fix
1 parent 6963a68 commit 9461490

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

Directory.Packages.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.4" />
2121
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
2222
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />
23+
24+
<PackageVersion Include="EntityFrameworkCore.FSharp" Version="6.0.7" />
25+
<PackageVersion Include="FSharp.Core" Version="6.0.5" />
2326
</ItemGroup>
2427
</Project>

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public override int GetHashCode()
523523
var objExpr = new SqlTransparentExpression(Expression.Constant(DefaultValue.GetValue(type), type), _mappingSource?.FindMapping(propInfo));
524524

525525
var newExpression = _dependencies.MemberTranslatorProvider.Translate(objExpr, propInfo, propInfo.GetMemberType(), _logger!);
526-
if (newExpression != null)
526+
if (newExpression != null && newExpression != objExpr)
527527
{
528528
var parametersArray = new Expression[] { objExpr };
529529
result = ConvertToExpressionAttribute(propInfo, newExpression, parametersArray);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\Build\linq2db.Tests.props" />
4+
5+
<PropertyGroup>
6+
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Tests.fs" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\Source\LinqToDB.EntityFrameworkCore\linq2db.EntityFrameworkCore.csproj" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
16+
<PackageReference Include="EntityFrameworkCore.FSharp" />
17+
<PackageReference Include="FSharp.Core" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module LinqToDB.EntityFrameworkCore.FSharpTests
2+
3+
open System.Linq
4+
open LinqToDB
5+
open System.ComponentModel.DataAnnotations
6+
open System.ComponentModel.DataAnnotations.Schema
7+
open Microsoft.EntityFrameworkCore
8+
open EntityFrameworkCore.FSharp.Extensions
9+
open NUnit.Framework
10+
11+
[<CLIMutable>]
12+
type WithIdentity = {
13+
[<Key>]
14+
[<DatabaseGenerated(DatabaseGeneratedOption.Identity)>]
15+
Id : int
16+
Name : string
17+
}
18+
19+
type AppDbContext(options: DbContextOptions<AppDbContext>) =
20+
inherit DbContext(options)
21+
22+
[<DefaultValue>] val mutable WithIdentity : DbSet<WithIdentity>
23+
member this.CompaniesInformation
24+
with get() = this.WithIdentity
25+
and set v = this.WithIdentity <- v
26+
27+
type TestDbContextFactory() =
28+
member this.CreateDbContext() =
29+
let options = new DbContextOptionsBuilder<AppDbContext>()
30+
options.UseSqlite("DataSource=:memory:").UseFSharpTypes() |> ignore
31+
new AppDbContext(options.Options)
32+
33+
[<TestFixture>]
34+
type Tests() =
35+
36+
[<Test>]
37+
member this.TestLeftJoin() =
38+
let context = TestDbContextFactory().CreateDbContext()
39+
let q =
40+
context
41+
.WithIdentity
42+
.Join(
43+
context.WithIdentity,
44+
(fun p -> p.Id),
45+
(fun c -> c.Id),
46+
(fun p c ->
47+
{|
48+
Person = p
49+
Company = c
50+
|}) )
51+
.LeftJoin(
52+
context.WithIdentity,
53+
(fun partialPerson cInfo -> partialPerson.Company.Id = cInfo.Id),
54+
(fun partialPerson cInfo ->
55+
{|
56+
Company = partialPerson.Company
57+
CompanyInformation = cInfo
58+
Person = partialPerson.Person
59+
|}) )
60+
//q.ToArray() |> ignore
61+
q.ToLinqToDB().ToString() |> ignore

linq2db.EFCore.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqToDB.EntityFrameworkCor
4141
EndProject
4242
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqToDB.EntityFrameworkCore.SQLite.Tests", "Tests\LinqToDB.EntityFrameworkCore.SQLite.Tests\LinqToDB.EntityFrameworkCore.SQLite.Tests.csproj", "{615C5697-5FA7-490C-8812-3D61994A8AC1}"
4343
EndProject
44+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "LinqToDB.EntityFrameworkCore.FSharpTests", "Tests\LinqToDB.EntityFrameworkCore.FSharpTests\LinqToDB.EntityFrameworkCore.FSharpTests.fsproj", "{ECF4637B-1C93-4D87-BDF2-E7F4DF7ED5C9}"
45+
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4648
Debug|Any CPU = Debug|Any CPU
@@ -71,6 +73,10 @@ Global
7173
{615C5697-5FA7-490C-8812-3D61994A8AC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
7274
{615C5697-5FA7-490C-8812-3D61994A8AC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
7375
{615C5697-5FA7-490C-8812-3D61994A8AC1}.Release|Any CPU.Build.0 = Release|Any CPU
76+
{ECF4637B-1C93-4D87-BDF2-E7F4DF7ED5C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
77+
{ECF4637B-1C93-4D87-BDF2-E7F4DF7ED5C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
78+
{ECF4637B-1C93-4D87-BDF2-E7F4DF7ED5C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
79+
{ECF4637B-1C93-4D87-BDF2-E7F4DF7ED5C9}.Release|Any CPU.Build.0 = Release|Any CPU
7480
EndGlobalSection
7581
GlobalSection(SolutionProperties) = preSolution
7682
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)