diff --git a/src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs b/src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs index 03f2b4b5..bf111603 100644 --- a/src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs +++ b/src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs @@ -45,11 +45,5 @@ namespace OpenIddict { /// associated with the current authorization. /// public virtual IList Tokens { get; } = new List(); - - /// - /// Gets or sets the identifier of the user profile - /// associated with the current authorization. - /// - public virtual TKey UserId { get; set; } } } diff --git a/src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs b/src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs index 487830ec..6b14f73e 100644 --- a/src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs +++ b/src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs @@ -21,13 +21,6 @@ namespace OpenIddict { /// Represents an OpenIddict token. /// public class OpenIddictToken where TKey : IEquatable { - /// - /// Gets or sets the identifier of the authorization attached with the current token. - /// This property may be null if the token was issued without - /// requiring the user consent or is bound to a client application. - /// - public virtual TKey AuthorizationId { get; set; } - /// /// Gets or sets the unique identifier /// associated with the current token. @@ -38,11 +31,5 @@ namespace OpenIddict { /// Gets or sets the type of the current token. /// public virtual string Type { get; set; } - - /// - /// Gets or sets the identifier of the user attached with the current token. - /// This property is null if the token represents a client application. - /// - public virtual TKey UserId { get; set; } } } diff --git a/src/OpenIddict.EntityFramework/OpenIddictContext.cs b/src/OpenIddict.EntityFramework/OpenIddictContext.cs index 87b28970..a6957abb 100644 --- a/src/OpenIddict.EntityFramework/OpenIddictContext.cs +++ b/src/OpenIddict.EntityFramework/OpenIddictContext.cs @@ -127,6 +127,10 @@ namespace OpenIddict { protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); + // 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. + // Configure the TApplication entity. builder.Entity(entity => { entity.HasKey(application => application.Id); @@ -140,7 +144,7 @@ namespace OpenIddict { entity.HasMany(authorization => authorization.Tokens) .WithOne() - .HasForeignKey(token => token.AuthorizationId) + .HasForeignKey("AuthorizationId") .IsRequired(required: false); entity.ToTable("OpenIddictAuthorizations"); @@ -164,12 +168,12 @@ namespace OpenIddict { builder.Entity(entity => { entity.HasMany(user => user.Authorizations) .WithOne() - .HasForeignKey(authorization => authorization.UserId) + .HasForeignKey("UserId") .IsRequired(required: false); entity.HasMany(user => user.Tokens) .WithOne() - .HasForeignKey(token => token.UserId) + .HasForeignKey("UserId") .IsRequired(required: false); }); }