/*
* 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.ComponentModel;
using System.Data.Entity.ModelConfiguration;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using OpenIddict.EntityFramework.Models;
namespace OpenIddict.EntityFramework;
///
/// Defines a relational mapping for the application entity.
///
/// The type of the application entity.
/// The type of the authorization entity.
/// The type of the session entity.
/// The type of the token entity.
/// The type of the primary key.
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class OpenIddictEntityFrameworkApplicationConfiguration<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TApplication,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TAuthorization,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSession,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TToken,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TKey> : EntityTypeConfiguration
where TApplication : OpenIddictEntityFrameworkApplication
where TAuthorization : OpenIddictEntityFrameworkAuthorization
where TSession : OpenIddictEntityFrameworkSession
where TToken : OpenIddictEntityFrameworkToken
where TKey : notnull, IEquatable
{
public OpenIddictEntityFrameworkApplicationConfiguration()
{
// 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.
HasMany(static application => application.Authorizations)
.WithOptional(static authorization => authorization.Application!)
.Map(static association =>
{
association.MapKey(nameof(OpenIddictEntityFrameworkAuthorization.Application) +
nameof(OpenIddictEntityFrameworkApplication.Id));
});
Property(static application => application.ApplicationType)
.HasMaxLength(50);
Property(static application => application.ClientId)
.HasMaxLength(100);
HasIndex(static application => application.ClientId)
.IsUnique();
Property(static application => application.ClientType)
.HasMaxLength(50);
Property(static application => application.ConcurrencyToken)
.HasMaxLength(50)
.IsConcurrencyToken();
Property(static application => application.ConsentType)
.HasMaxLength(50);
HasKey(static application => application.Id);
if (typeof(TKey) == typeof(string))
{
var parameter = Expression.Parameter(typeof(TApplication), "application");
var property = Expression.Property(parameter,
typeof(TApplication).GetProperty(nameof(OpenIddictEntityFrameworkApplication.Id))!);
var lambda = Expression.Lambda>(property, parameter);
Property(lambda).HasMaxLength(100);
}
HasMany(static application => application.Sessions)
.WithOptional(static session => session.Application!)
.Map(static association =>
{
association.MapKey(nameof(OpenIddictEntityFrameworkSession.Application) +
nameof(OpenIddictEntityFrameworkApplication.Id));
});
HasMany(static application => application.Tokens)
.WithOptional(static token => token.Application!)
.Map(static association =>
{
association.MapKey(nameof(OpenIddictEntityFrameworkToken.Application) +
nameof(OpenIddictEntityFrameworkApplication.Id));
});
ToTable("OpenIddictApplications");
}
}