Browse Source

Update the EF 6.x/EF Core entity configurations to use pre-defined column lengths

pull/711/head
Kévin Chalet 8 years ago
parent
commit
3a13c8505a
  1. 9
      src/OpenIddict.EntityFramework/Configurations/OpenIddictApplicationConfiguration.cs
  2. 4
      src/OpenIddict.EntityFramework/Configurations/OpenIddictAuthorizationConfiguration.cs
  3. 8
      src/OpenIddict.EntityFramework/Configurations/OpenIddictScopeConfiguration.cs
  4. 12
      src/OpenIddict.EntityFramework/Configurations/OpenIddictTokenConfiguration.cs
  5. 19
      src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictApplicationConfiguration.cs
  6. 23
      src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictAuthorizationConfiguration.cs
  7. 14
      src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictScopeConfiguration.cs
  8. 32
      src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictTokenConfiguration.cs

9
src/OpenIddict.EntityFramework/Configurations/OpenIddictApplicationConfiguration.cs

@ -48,14 +48,19 @@ namespace OpenIddict.EntityFramework
HasKey(application => application.Id);
Property(application => application.ClientId)
.HasMaxLength(100)
.IsRequired()
.HasMaxLength(450)
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute
{
IsUnique = true
}));
Property(application => application.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
Property(application => application.Type)
.HasMaxLength(25)
.IsRequired();
HasMany(application => application.Authorizations)

4
src/OpenIddict.EntityFramework/Configurations/OpenIddictAuthorizationConfiguration.cs

@ -46,15 +46,19 @@ namespace OpenIddict.EntityFramework
HasKey(authorization => authorization.Id);
Property(authorization => authorization.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
Property(authorization => authorization.Status)
.HasMaxLength(25)
.IsRequired();
Property(authorization => authorization.Subject)
.HasMaxLength(450)
.IsRequired();
Property(authorization => authorization.Type)
.HasMaxLength(25)
.IsRequired();
HasMany(authorization => authorization.Tokens)

8
src/OpenIddict.EntityFramework/Configurations/OpenIddictScopeConfiguration.cs

@ -44,12 +44,16 @@ namespace OpenIddict.EntityFramework
HasKey(scope => scope.Id);
Property(scope => scope.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
Property(scope => scope.Name)
.HasMaxLength(200)
.IsRequired()
.HasMaxLength(450)
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute
{
IsUnique = true
}));
ToTable("OpenIddictScopes");
}

12
src/OpenIddict.EntityFramework/Configurations/OpenIddictTokenConfiguration.cs

@ -48,16 +48,26 @@ namespace OpenIddict.EntityFramework
HasKey(token => token.Id);
Property(token => token.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
// Warning: the index on the ReferenceId property MUST NOT be declared as
// a unique index, as Entity Framework 6.x doesn't support creating indexes
// with null-friendly WHERE conditions, unlike Entity Framework Core 1.x/2.x.
Property(token => token.ReferenceId)
.HasMaxLength(450)
.HasMaxLength(100)
.HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));
Property(token => token.Status)
.HasMaxLength(25)
.IsRequired();
Property(token => token.Subject)
.HasMaxLength(450)
.IsRequired();
Property(token => token.Type)
.HasMaxLength(25)
.IsRequired();
ToTable("OpenIddictTokens");

19
src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictApplicationConfiguration.cs

@ -6,6 +6,7 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OpenIddict.EntityFrameworkCore.Models;
@ -26,7 +27,7 @@ namespace OpenIddict.EntityFrameworkCore
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
public void Configure(EntityTypeBuilder<TApplication> builder)
public void Configure([NotNull] EntityTypeBuilder<TApplication> builder)
{
if (builder == null)
{
@ -37,28 +38,40 @@ namespace OpenIddict.EntityFrameworkCore
// Entity Framework would throw an exception due to the TKey generic parameter
// being non-nullable when using value types like short, int, long or Guid.
// If primary/foreign keys are strings, limit their length to ensure
// they can be safely used in indexes, specially when the underlying
// provider is known to not restrict the default length (e.g MySQL).
if (typeof(TKey) == typeof(string))
{
builder.Property(application => application.Id)
.HasMaxLength(50);
}
builder.HasKey(application => application.Id);
builder.HasIndex(application => application.ClientId)
.IsUnique();
builder.Property(application => application.ClientId)
.HasMaxLength(100)
.IsRequired();
builder.Property(application => application.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
builder.Property(application => application.Type)
.HasMaxLength(25)
.IsRequired();
builder.HasMany(application => application.Authorizations)
.WithOne(authorization => authorization.Application)
.HasForeignKey("ApplicationId")
.HasForeignKey(nameof(OpenIddictAuthorization.Application) + nameof(OpenIddictApplication.Id))
.IsRequired(required: false);
builder.HasMany(application => application.Tokens)
.WithOne(token => token.Application)
.HasForeignKey("ApplicationId")
.HasForeignKey(nameof(OpenIddictToken.Application) + nameof(OpenIddictApplication.Id))
.IsRequired(required: false);
builder.ToTable("OpenIddictApplications");

23
src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictAuthorizationConfiguration.cs

@ -6,6 +6,7 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OpenIddict.EntityFrameworkCore.Models;
@ -26,7 +27,7 @@ namespace OpenIddict.EntityFrameworkCore
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
public void Configure(EntityTypeBuilder<TAuthorization> builder)
public void Configure([NotNull] EntityTypeBuilder<TAuthorization> builder)
{
if (builder == null)
{
@ -37,24 +38,40 @@ namespace OpenIddict.EntityFrameworkCore
// Entity Framework would throw an exception due to the TKey generic parameter
// being non-nullable when using value types like short, int, long or Guid.
// If primary/foreign keys are strings, limit their length to ensure
// they can be safely used in indexes, specially when the underlying
// provider is known to not restrict the default length (e.g MySQL).
if (typeof(TKey) == typeof(string))
{
builder.Property(nameof(OpenIddictAuthorization.Application) + nameof(OpenIddictApplication.Id))
.HasMaxLength(50);
builder.Property(application => application.Id)
.HasMaxLength(50);
}
builder.HasKey(authorization => authorization.Id);
builder.HasIndex("ApplicationId",
nameof(OpenIddictAuthorization.Scopes),
builder.HasIndex(
nameof(OpenIddictAuthorization.Application) + nameof(OpenIddictApplication.Id),
nameof(OpenIddictAuthorization.Status),
nameof(OpenIddictAuthorization.Subject),
nameof(OpenIddictAuthorization.Type));
builder.Property(authorization => authorization.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
builder.Property(authorization => authorization.Status)
.HasMaxLength(25)
.IsRequired();
builder.Property(authorization => authorization.Subject)
.HasMaxLength(450)
.IsRequired();
builder.Property(authorization => authorization.Type)
.HasMaxLength(25)
.IsRequired();
builder.HasMany(authorization => authorization.Tokens)

14
src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictScopeConfiguration.cs

@ -6,6 +6,7 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OpenIddict.EntityFrameworkCore.Models;
@ -22,7 +23,7 @@ namespace OpenIddict.EntityFrameworkCore
where TScope : OpenIddictScope<TKey>
where TKey : IEquatable<TKey>
{
public void Configure(EntityTypeBuilder<TScope> builder)
public void Configure([NotNull] EntityTypeBuilder<TScope> builder)
{
if (builder == null)
{
@ -33,15 +34,26 @@ namespace OpenIddict.EntityFrameworkCore
// Entity Framework would throw an exception due to the TKey generic parameter
// being non-nullable when using value types like short, int, long or Guid.
// If primary/foreign keys are strings, limit their length to ensure
// they can be safely used in indexes, specially when the underlying
// provider is known to not restrict the default length (e.g MySQL).
if (typeof(TKey) == typeof(string))
{
builder.Property(scope => scope.Id)
.HasMaxLength(50);
}
builder.HasKey(scope => scope.Id);
builder.HasIndex(scope => scope.Name)
.IsUnique();
builder.Property(scope => scope.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
builder.Property(scope => scope.Name)
.HasMaxLength(200)
.IsRequired();
builder.ToTable("OpenIddictScopes");

32
src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictTokenConfiguration.cs

@ -6,6 +6,7 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using OpenIddict.EntityFrameworkCore.Models;
@ -26,7 +27,7 @@ namespace OpenIddict.EntityFrameworkCore
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TKey : IEquatable<TKey>
{
public void Configure(EntityTypeBuilder<TToken> builder)
public void Configure([NotNull] EntityTypeBuilder<TToken> builder)
{
if (builder == null)
{
@ -37,23 +38,50 @@ namespace OpenIddict.EntityFrameworkCore
// Entity Framework would throw an exception due to the TKey generic parameter
// being non-nullable when using value types like short, int, long or Guid.
// If primary/foreign keys are strings, limit their length to ensure
// they can be safely used in indexes, specially when the underlying
// provider is known to not restrict the default length (e.g MySQL).
if (typeof(TKey) == typeof(string))
{
builder.Property(nameof(OpenIddictToken.Application) + nameof(OpenIddictApplication.Id))
.HasMaxLength(50);
builder.Property(nameof(OpenIddictToken.Authorization) + nameof(OpenIddictApplication.Id))
.HasMaxLength(50);
builder.Property(token => token.Id)
.HasMaxLength(50);
}
builder.HasKey(token => token.Id);
builder.HasIndex(token => token.ReferenceId)
.IsUnique();
builder.HasIndex("ApplicationId",
builder.HasIndex(
nameof(OpenIddictToken.Application) + nameof(OpenIddictApplication.Id),
nameof(OpenIddictToken.Status),
nameof(OpenIddictToken.Subject),
nameof(OpenIddictToken.Type));
builder.Property(token => token.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
builder.Property(token => token.ReferenceId)
.HasMaxLength(100);
builder.Property(token => token.Status)
.HasMaxLength(25)
.IsRequired();
builder.Property(token => token.Subject)
.HasMaxLength(450)
.IsRequired();
builder.Property(token => token.Type)
.HasMaxLength(25)
.IsRequired();
builder.ToTable("OpenIddictTokens");

Loading…
Cancel
Save