Skip to content

Commit 4b4bb76

Browse files
author
David Fallah
committed
Multi-target .NET Standard 2.1, .NET 5 and .NET 6
1 parent 939f5ae commit 4b4bb76

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

src/SpatialFocus.EntityFrameworkCore.Extensions/NamingExtension.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ public static void ConfigureNames(this ModelBuilder modelBuilder, NamingOptions
2929
}
3030

3131
// Properties
32+
#if NET5_0_OR_GREATER
3233
entity.GetProperties()
3334
.ToList()
3435
.ForEach(x => x.SetColumnName(namingOptions.ColumnNamingFunction(x.GetColumnBaseName())));
36+
#else
37+
entity.GetProperties()
38+
.ToList()
39+
.ForEach(x => x.SetColumnName(namingOptions.ColumnNamingFunction(x.GetColumnName())));
40+
#endif
3541

3642
// Primary and Alternative keys
3743
entity.GetKeys().ToList().ForEach(x => x.SetName(namingOptions.ConstraintNamingFunction(x.GetName())));
@@ -42,9 +48,15 @@ public static void ConfigureNames(this ModelBuilder modelBuilder, NamingOptions
4248
.ForEach(x => x.SetConstraintName(namingOptions.ConstraintNamingFunction(x.GetConstraintName())));
4349

4450
// Indices
51+
#if NET5_0_OR_GREATER
4552
entity.GetIndexes()
4653
.ToList()
4754
.ForEach(x => x.SetDatabaseName(namingOptions.ConstraintNamingFunction(x.GetDatabaseName())));
55+
#else
56+
entity.GetIndexes()
57+
.ToList()
58+
.ForEach(x => x.SetName(namingOptions.ConstraintNamingFunction(x.GetName())));
59+
#endif
4860
}
4961
}
5062
}

src/SpatialFocus.EntityFrameworkCore.Extensions/SpatialFocus.EntityFrameworkCore.Extensions.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.1;net5.0;net6.0</TargetFrameworks>
55
</PropertyGroup>
66

77
<PropertyGroup>
@@ -33,6 +33,22 @@
3333
<ItemGroup>
3434
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
3535
<PackageReference Include="Humanizer.Core" Version="2.8.26" />
36+
</ItemGroup>
37+
38+
<!-- Conditionally obtain references for the netstandard2.1 target -->
39+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
40+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
41+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
42+
</ItemGroup>
43+
44+
<!-- Conditionally obtain references for the net5.0 target -->
45+
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
46+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.12" />
47+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.12" />
48+
</ItemGroup>
49+
50+
<!-- Conditionally obtain references for the net6.0 target -->
51+
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
3652
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
3753
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
3854
</ItemGroup>

test/SpatialFocus.EntityFrameworkCore.Extensions.Test/NamingOptionsTest.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,42 @@ protected ProductContext GetContext(EnumLookupOptions? enumLookupOptions = null,
2828
return context;
2929
}
3030

31+
#if NET5_0_OR_GREATER
3132
[Fact]
3233
public void OverrideColumnNaming()
3334
{
3435
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideColumnNaming(NamingScheme.SnakeCase));
3536

3637
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
38+
39+
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnBaseName());
40+
}
41+
#else
42+
[Fact]
43+
public void OverrideColumnNaming()
44+
{
45+
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideColumnNaming(NamingScheme.SnakeCase));
46+
47+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
48+
3749
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnName());
3850
}
51+
#endif
3952

53+
#if NET5_0_OR_GREATER
54+
[Fact]
55+
public void OverrideConstraintNaming()
56+
{
57+
ProductContext context = GetContext(namingOptions: new NamingOptions().OverrideConstraintNaming(NamingScheme.SnakeCase));
58+
59+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
60+
Assert.Equal("ProductTag", findEntityType.GetTableName());
61+
Assert.Equal("ProductTagId", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnBaseName());
62+
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
63+
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
64+
Assert.True(findEntityType.GetIndexes().All(x => x.GetDatabaseName() == NamingScheme.SnakeCase(x.GetDefaultDatabaseName())));
65+
}
66+
#else
4067
[Fact]
4168
public void OverrideConstraintNaming()
4269
{
@@ -49,6 +76,7 @@ public void OverrideConstraintNaming()
4976
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
5077
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
5178
}
79+
#endif
5280

5381
[Fact]
5482
public void OverrideTableNaming()
@@ -77,6 +105,20 @@ public void PluralizeAndOverrideTableNaming()
77105
Assert.Equal("product_tags", findEntityType.GetTableName());
78106
}
79107

108+
#if NET5_0_OR_GREATER
109+
[Fact]
110+
public void SetNamingScheme()
111+
{
112+
ProductContext context = GetContext(namingOptions: new NamingOptions().SetNamingScheme(NamingScheme.SnakeCase));
113+
114+
IEntityType findEntityType = context.Model.FindEntityType(typeof(ProductTag));
115+
Assert.Equal("product_tag", findEntityType.GetTableName());
116+
Assert.Equal("product_tag_id", findEntityType.FindProperty(nameof(ProductTag.ProductTagId)).GetColumnBaseName());
117+
Assert.True(findEntityType.GetKeys().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
118+
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
119+
Assert.True(findEntityType.GetIndexes().All(x => x.GetDatabaseName() == NamingScheme.SnakeCase(x.GetDefaultDatabaseName())));
120+
}
121+
#else
80122
[Fact]
81123
public void SetNamingScheme()
82124
{
@@ -89,6 +131,7 @@ public void SetNamingScheme()
89131
Assert.True(findEntityType.GetForeignKeys().All(x => x.GetConstraintName() == NamingScheme.SnakeCase(x.GetDefaultName())));
90132
Assert.True(findEntityType.GetIndexes().All(x => x.GetName() == NamingScheme.SnakeCase(x.GetDefaultName())));
91133
}
134+
#endif
92135

93136
[Fact]
94137
public void MultipleProviders()
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
5-
6-
<IsPackable>false</IsPackable>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
87

98
<ItemGroup>
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
129
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1310
<PackageReference Include="xunit" Version="2.4.0" />
1411
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1512
<PackageReference Include="coverlet.collector" Version="1.0.1" />
1613
</ItemGroup>
1714

18-
<ItemGroup>
19-
<ProjectReference Include="..\..\src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj" />
20-
</ItemGroup>
15+
<!-- Conditionally obtain references for the netcoreapp3.1 target -->
16+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
17+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
19+
</ItemGroup>
20+
21+
<!-- Conditionally obtain references for the net5.0 target -->
22+
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
23+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.12" />
24+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.12" />
25+
</ItemGroup>
26+
27+
<!-- Conditionally obtain references for the net6.0 target -->
28+
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
29+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
30+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\..\src\SpatialFocus.EntityFrameworkCore.Extensions\SpatialFocus.EntityFrameworkCore.Extensions.csproj" />
35+
</ItemGroup>
2136

2237
</Project>

0 commit comments

Comments
 (0)