Skip to content

Commit 3aea683

Browse files
committed
Reduce the maximum lengh of string primary keys in the EF 6 and EF Core stores
1 parent 99fca3f commit 3aea683

8 files changed

+128
-60
lines changed

src/OpenIddict.EntityFramework/Configurations/OpenIddictEntityFrameworkApplicationConfiguration.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Data.Entity.Infrastructure.Annotations;
1010
using System.Data.Entity.ModelConfiguration;
1111
using System.Diagnostics.CodeAnalysis;
12+
using System.Linq.Expressions;
1213
using OpenIddict.EntityFramework.Models;
1314

1415
namespace OpenIddict.EntityFramework;
@@ -37,39 +38,49 @@ public OpenIddictEntityFrameworkApplicationConfiguration()
3738
// Entity Framework would throw an exception due to the TKey generic parameter
3839
// being non-nullable when using value types like short, int, long or Guid.
3940

40-
HasKey(application => application.Id);
41+
HasKey(static application => application.Id);
4142

42-
Property(application => application.ApplicationType)
43+
Property(static application => application.ApplicationType)
4344
.HasMaxLength(50);
4445

45-
Property(application => application.ClientId)
46+
Property(static application => application.ClientId)
4647
.HasMaxLength(100)
4748
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute
4849
{
4950
IsUnique = true
5051
}));
5152

52-
Property(application => application.ClientType)
53+
Property(static application => application.ClientType)
5354
.HasMaxLength(50);
5455

55-
Property(application => application.ConcurrencyToken)
56+
Property(static application => application.ConcurrencyToken)
5657
.HasMaxLength(50)
5758
.IsConcurrencyToken();
5859

59-
Property(application => application.ConsentType)
60+
Property(static application => application.ConsentType)
6061
.HasMaxLength(50);
6162

62-
HasMany(application => application.Authorizations)
63-
.WithOptional(authorization => authorization.Application!)
64-
.Map(association =>
63+
if (typeof(TKey) == typeof(string))
64+
{
65+
var parameter = Expression.Parameter(typeof(TApplication), "application");
66+
var property = Expression.Property(parameter,
67+
typeof(TApplication).GetProperty(nameof(OpenIddictEntityFrameworkApplication.Id))!);
68+
var lambda = Expression.Lambda<Func<TApplication, string>>(property, parameter);
69+
70+
Property(lambda).HasMaxLength(100);
71+
}
72+
73+
HasMany(static application => application.Authorizations)
74+
.WithOptional(static authorization => authorization.Application!)
75+
.Map(static association =>
6576
{
6677
association.MapKey(nameof(OpenIddictEntityFrameworkAuthorization.Application) +
6778
nameof(OpenIddictEntityFrameworkApplication.Id));
6879
});
6980

70-
HasMany(application => application.Tokens)
71-
.WithOptional(token => token.Application!)
72-
.Map(association =>
81+
HasMany(static application => application.Tokens)
82+
.WithOptional(static token => token.Application!)
83+
.Map(static association =>
7384
{
7485
association.MapKey(nameof(OpenIddictEntityFrameworkToken.Application) +
7586
nameof(OpenIddictEntityFrameworkApplication.Id));

src/OpenIddict.EntityFramework/Configurations/OpenIddictEntityFrameworkAuthorizationConfiguration.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using System.Data.Entity.ModelConfiguration;
99
using System.Diagnostics.CodeAnalysis;
10+
using System.Linq.Expressions;
1011
using OpenIddict.EntityFramework.Models;
1112

1213
namespace OpenIddict.EntityFramework;
@@ -35,25 +36,35 @@ public OpenIddictEntityFrameworkAuthorizationConfiguration()
3536
// Entity Framework would throw an exception due to the TKey generic parameter
3637
// being non-nullable when using value types like short, int, long or Guid.
3738

38-
HasKey(authorization => authorization.Id);
39+
HasKey(static authorization => authorization.Id);
3940

40-
Property(authorization => authorization.ConcurrencyToken)
41+
Property(static authorization => authorization.ConcurrencyToken)
4142
.HasMaxLength(50)
4243
.IsConcurrencyToken();
4344

44-
Property(authorization => authorization.Status)
45+
if (typeof(TKey) == typeof(string))
46+
{
47+
var parameter = Expression.Parameter(typeof(TAuthorization), "authorization");
48+
var property = Expression.Property(parameter,
49+
typeof(TAuthorization).GetProperty(nameof(OpenIddictEntityFrameworkAuthorization.Id))!);
50+
var lambda = Expression.Lambda<Func<TAuthorization, string>>(property, parameter);
51+
52+
Property(lambda).HasMaxLength(100);
53+
}
54+
55+
Property(static authorization => authorization.Status)
4556
.HasMaxLength(50);
4657

47-
Property(authorization => authorization.Subject)
58+
Property(static authorization => authorization.Subject)
4859
.HasMaxLength(400);
4960

50-
Property(authorization => authorization.Type)
61+
Property(static authorization => authorization.Type)
5162
.HasMaxLength(50);
5263

53-
HasMany(authorization => authorization.Tokens)
54-
.WithOptional(token => token.Authorization!)
55-
.Map(association => association.MapKey(nameof(OpenIddictEntityFrameworkToken.Authorization) +
56-
nameof(OpenIddictEntityFrameworkAuthorization.Id)))
64+
HasMany(static authorization => authorization.Tokens)
65+
.WithOptional(static token => token.Authorization!)
66+
.Map(static association => association.MapKey(nameof(OpenIddictEntityFrameworkToken.Authorization) +
67+
nameof(OpenIddictEntityFrameworkAuthorization.Id)))
5768
.WillCascadeOnDelete();
5869

5970
ToTable("OpenIddictAuthorizations");

src/OpenIddict.EntityFramework/Configurations/OpenIddictEntityFrameworkScopeConfiguration.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Data.Entity.Infrastructure.Annotations;
1010
using System.Data.Entity.ModelConfiguration;
1111
using System.Diagnostics.CodeAnalysis;
12+
using System.Linq.Expressions;
1213
using OpenIddict.EntityFramework.Models;
1314

1415
namespace OpenIddict.EntityFramework;
@@ -31,13 +32,23 @@ public OpenIddictEntityFrameworkScopeConfiguration()
3132
// Entity Framework would throw an exception due to the TKey generic parameter
3233
// being non-nullable when using value types like short, int, long or Guid.
3334

34-
HasKey(scope => scope.Id);
35+
HasKey(static scope => scope.Id);
3536

36-
Property(scope => scope.ConcurrencyToken)
37+
Property(static scope => scope.ConcurrencyToken)
3738
.HasMaxLength(50)
3839
.IsConcurrencyToken();
3940

40-
Property(scope => scope.Name)
41+
if (typeof(TKey) == typeof(string))
42+
{
43+
var parameter = Expression.Parameter(typeof(TScope), "scope");
44+
var property = Expression.Property(parameter,
45+
typeof(TScope).GetProperty(nameof(OpenIddictEntityFrameworkScope.Id))!);
46+
var lambda = Expression.Lambda<Func<TScope, string>>(property, parameter);
47+
48+
Property(lambda).HasMaxLength(100);
49+
}
50+
51+
Property(static scope => scope.Name)
4152
.HasMaxLength(200)
4253
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute
4354
{

src/OpenIddict.EntityFramework/Configurations/OpenIddictEntityFrameworkTokenConfiguration.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Data.Entity.Infrastructure.Annotations;
1010
using System.Data.Entity.ModelConfiguration;
1111
using System.Diagnostics.CodeAnalysis;
12+
using System.Linq.Expressions;
1213
using OpenIddict.EntityFramework.Models;
1314

1415
namespace OpenIddict.EntityFramework;
@@ -37,26 +38,36 @@ public OpenIddictEntityFrameworkTokenConfiguration()
3738
// Entity Framework would throw an exception due to the TKey generic parameter
3839
// being non-nullable when using value types like short, int, long or Guid.
3940

40-
HasKey(token => token.Id);
41+
HasKey(static token => token.Id);
4142

42-
Property(token => token.ConcurrencyToken)
43+
Property(static token => token.ConcurrencyToken)
4344
.HasMaxLength(50)
4445
.IsConcurrencyToken();
4546

47+
if (typeof(TKey) == typeof(string))
48+
{
49+
var parameter = Expression.Parameter(typeof(TToken), "token");
50+
var property = Expression.Property(parameter,
51+
typeof(TToken).GetProperty(nameof(OpenIddictEntityFrameworkToken.Id))!);
52+
var lambda = Expression.Lambda<Func<TToken, string>>(property, parameter);
53+
54+
Property(lambda).HasMaxLength(100);
55+
}
56+
4657
// Warning: the index on the ReferenceId property MUST NOT be declared as
4758
// a unique index, as Entity Framework 6.x doesn't support creating indexes
4859
// with null-friendly WHERE conditions, unlike Entity Framework Core.
49-
Property(token => token.ReferenceId)
60+
Property(static token => token.ReferenceId)
5061
.HasMaxLength(100)
5162
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
5263

53-
Property(token => token.Status)
64+
Property(static token => token.Status)
5465
.HasMaxLength(50);
5566

56-
Property(token => token.Subject)
67+
Property(static token => token.Subject)
5768
.HasMaxLength(400);
5869

59-
Property(token => token.Type)
70+
Property(static token => token.Type)
6071
.HasMaxLength(150);
6172

6273
ToTable("OpenIddictTokens");

src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreApplicationConfiguration.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public void Configure(EntityTypeBuilder<TApplication> builder)
4040
// Entity Framework would throw an exception due to the TKey generic parameter
4141
// being non-nullable when using value types like short, int, long or Guid.
4242

43-
builder.HasKey(application => application.Id);
43+
builder.HasKey(static application => application.Id);
4444

45-
builder.Property(application => application.ApplicationType)
45+
builder.Property(static application => application.ApplicationType)
4646
.HasMaxLength(50);
4747

4848
// Warning: the non-generic overlord is deliberately used to work around
@@ -51,30 +51,36 @@ public void Configure(EntityTypeBuilder<TApplication> builder)
5151
builder.HasIndex(nameof(OpenIddictEntityFrameworkCoreApplication.ClientId))
5252
.IsUnique();
5353

54-
builder.Property(application => application.ClientId)
54+
builder.Property(static application => application.ClientId)
5555
.HasMaxLength(100);
5656

57-
builder.Property(application => application.ClientType)
57+
builder.Property(static application => application.ClientType)
5858
.HasMaxLength(50);
5959

60-
builder.Property(application => application.ConcurrencyToken)
60+
builder.Property(static application => application.ConcurrencyToken)
6161
.HasMaxLength(50)
6262
.IsConcurrencyToken();
6363

64-
builder.Property(application => application.ConsentType)
64+
builder.Property(static application => application.ConsentType)
6565
.HasMaxLength(50);
6666

67-
builder.Property(application => application.Id)
67+
builder.Property(static application => application.Id)
6868
.ValueGeneratedOnAdd();
6969

70-
builder.HasMany(application => application.Authorizations)
71-
.WithOne(authorization => authorization.Application!)
70+
if (typeof(TKey) == typeof(string))
71+
{
72+
builder.Property(static application => application.Id)
73+
.HasMaxLength(100);
74+
}
75+
76+
builder.HasMany(static application => application.Authorizations)
77+
.WithOne(static authorization => authorization.Application!)
7278
.HasForeignKey(nameof(OpenIddictEntityFrameworkCoreAuthorization.Application) +
7379
nameof(OpenIddictEntityFrameworkCoreApplication.Id))
7480
.IsRequired(required: false);
7581

76-
builder.HasMany(application => application.Tokens)
77-
.WithOne(token => token.Application!)
82+
builder.HasMany(static application => application.Tokens)
83+
.WithOne(static token => token.Application!)
7884
.HasForeignKey(nameof(OpenIddictEntityFrameworkCoreToken.Application) + nameof(OpenIddictEntityFrameworkCoreApplication.Id))
7985
.IsRequired(required: false);
8086

src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreAuthorizationConfiguration.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,38 @@ public void Configure(EntityTypeBuilder<TAuthorization> builder)
4040
// Entity Framework would throw an exception due to the TKey generic parameter
4141
// being non-nullable when using value types like short, int, long or Guid.
4242

43-
builder.HasKey(authorization => authorization.Id);
43+
builder.HasKey(static authorization => authorization.Id);
4444

4545
builder.HasIndex(
4646
nameof(OpenIddictEntityFrameworkCoreAuthorization.Application) + nameof(OpenIddictEntityFrameworkCoreApplication.Id),
4747
nameof(OpenIddictEntityFrameworkCoreAuthorization.Status),
4848
nameof(OpenIddictEntityFrameworkCoreAuthorization.Subject),
4949
nameof(OpenIddictEntityFrameworkCoreAuthorization.Type));
5050

51-
builder.Property(authorization => authorization.ConcurrencyToken)
51+
builder.Property(static authorization => authorization.ConcurrencyToken)
5252
.HasMaxLength(50)
5353
.IsConcurrencyToken();
5454

55-
builder.Property(authorization => authorization.Id)
55+
builder.Property(static authorization => authorization.Id)
5656
.ValueGeneratedOnAdd();
5757

58-
builder.Property(authorization => authorization.Status)
58+
if (typeof(TKey) == typeof(string))
59+
{
60+
builder.Property(static authorization => authorization.Id)
61+
.HasMaxLength(100);
62+
}
63+
64+
builder.Property(static authorization => authorization.Status)
5965
.HasMaxLength(50);
6066

61-
builder.Property(authorization => authorization.Subject)
67+
builder.Property(static authorization => authorization.Subject)
6268
.HasMaxLength(400);
6369

64-
builder.Property(authorization => authorization.Type)
70+
builder.Property(static authorization => authorization.Type)
6571
.HasMaxLength(50);
6672

67-
builder.HasMany(authorization => authorization.Tokens)
68-
.WithOne(token => token.Authorization!)
73+
builder.HasMany(static authorization => authorization.Tokens)
74+
.WithOne(static token => token.Authorization!)
6975
.HasForeignKey(nameof(OpenIddictEntityFrameworkCoreToken.Authorization) +
7076
nameof(OpenIddictEntityFrameworkCoreAuthorization.Id))
7177
.IsRequired(required: false);

src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreScopeConfiguration.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,28 @@ public void Configure(EntityTypeBuilder<TScope> builder)
3434
// Entity Framework would throw an exception due to the TKey generic parameter
3535
// being non-nullable when using value types like short, int, long or Guid.
3636

37-
builder.HasKey(scope => scope.Id);
37+
builder.HasKey(static scope => scope.Id);
3838

3939
// Warning: the non-generic overlord is deliberately used to work around
4040
// a breaking change introduced in Entity Framework Core 3.x (where a
4141
// generic entity type builder is now returned by the HasIndex() method).
4242
builder.HasIndex(nameof(OpenIddictEntityFrameworkCoreScope.Name))
4343
.IsUnique();
4444

45-
builder.Property(scope => scope.ConcurrencyToken)
45+
builder.Property(static scope => scope.ConcurrencyToken)
4646
.HasMaxLength(50)
4747
.IsConcurrencyToken();
4848

49-
builder.Property(scope => scope.Id)
49+
builder.Property(static scope => scope.Id)
5050
.ValueGeneratedOnAdd();
5151

52-
builder.Property(scope => scope.Name)
52+
if (typeof(TKey) == typeof(string))
53+
{
54+
builder.Property(static scope => scope.Id)
55+
.HasMaxLength(100);
56+
}
57+
58+
builder.Property(static scope => scope.Name)
5359
.HasMaxLength(200);
5460

5561
builder.ToTable("OpenIddictScopes");

src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreTokenConfiguration.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Configure(EntityTypeBuilder<TToken> builder)
4040
// Entity Framework would throw an exception due to the TKey generic parameter
4141
// being non-nullable when using value types like short, int, long or Guid.
4242

43-
builder.HasKey(token => token.Id);
43+
builder.HasKey(static token => token.Id);
4444

4545
// Warning: the non-generic overlord is deliberately used to work around
4646
// a breaking change introduced in Entity Framework Core 3.x (where a
@@ -54,23 +54,29 @@ public void Configure(EntityTypeBuilder<TToken> builder)
5454
nameof(OpenIddictEntityFrameworkCoreToken.Subject),
5555
nameof(OpenIddictEntityFrameworkCoreToken.Type));
5656

57-
builder.Property(token => token.ConcurrencyToken)
57+
builder.Property(static token => token.ConcurrencyToken)
5858
.HasMaxLength(50)
5959
.IsConcurrencyToken();
6060

61-
builder.Property(token => token.Id)
61+
builder.Property(static token => token.Id)
6262
.ValueGeneratedOnAdd();
6363

64-
builder.Property(token => token.ReferenceId)
64+
if (typeof(TKey) == typeof(string))
65+
{
66+
builder.Property(static token => token.Id)
67+
.HasMaxLength(100);
68+
}
69+
70+
builder.Property(static token => token.ReferenceId)
6571
.HasMaxLength(100);
6672

67-
builder.Property(token => token.Status)
73+
builder.Property(static token => token.Status)
6874
.HasMaxLength(50);
6975

70-
builder.Property(token => token.Subject)
76+
builder.Property(static token => token.Subject)
7177
.HasMaxLength(400);
7278

73-
builder.Property(token => token.Type)
79+
builder.Property(static token => token.Type)
7480
.HasMaxLength(150);
7581

7682
builder.ToTable("OpenIddictTokens");

0 commit comments

Comments
 (0)