/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Infrastructure.Annotations; using System.Data.Entity.ModelConfiguration; using System.Text; using OpenIddict.EntityFramework.Models; namespace OpenIddict.EntityFramework { /// /// Defines a relational mapping for the Token entity. /// /// The type of the Token entity. /// The type of the Application entity. /// The type of the Authorization entity. /// The type of the Key entity. [EditorBrowsable(EditorBrowsableState.Never)] public class OpenIddictTokenConfiguration : EntityTypeConfiguration where TToken : OpenIddictToken where TApplication : OpenIddictApplication where TAuthorization : OpenIddictAuthorization where TKey : IEquatable { public OpenIddictTokenConfiguration() { // Note: unlike Entity Framework Core 1.x/2.x, Entity Framework 6.x // always throws an exception when using generic types as entity types. // To ensure a better exception is thrown, a manual check is made here. if (typeof(TToken).IsGenericType) { throw new InvalidOperationException(new StringBuilder() .AppendLine("The token entity cannot be a generic type.") .Append("Consider creating a non-generic derived class.") .ToString()); } // Warning: optional foreign keys MUST NOT be added as CLR properties because // 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. HasKey(token => token.Id); Property(token => token.ConcurrencyToken) .IsConcurrencyToken(); Property(token => token.ReferenceId) .HasMaxLength(450) .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute())); Property(token => token.Subject) .IsRequired(); Property(token => token.Type) .IsRequired(); ToTable("OpenIddictTokens"); } } }