Browse Source

Remove CLR foreign keys to prevent Entity Framework from throwing an exception when TKey is a value type

pull/130/head
Kévin Chalet 10 years ago
parent
commit
5b0b735c18
  1. 6
      src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs
  2. 13
      src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs
  3. 10
      src/OpenIddict.EntityFramework/OpenIddictContext.cs

6
src/OpenIddict.EntityFramework/Models/OpenIddictAuthorization.cs

@ -45,11 +45,5 @@ namespace OpenIddict {
/// associated with the current authorization. /// associated with the current authorization.
/// </summary> /// </summary>
public virtual IList<TToken> Tokens { get; } = new List<TToken>(); public virtual IList<TToken> Tokens { get; } = new List<TToken>();
/// <summary>
/// Gets or sets the identifier of the user profile
/// associated with the current authorization.
/// </summary>
public virtual TKey UserId { get; set; }
} }
} }

13
src/OpenIddict.EntityFramework/Models/OpenIddictToken.cs

@ -21,13 +21,6 @@ namespace OpenIddict {
/// Represents an OpenIddict token. /// Represents an OpenIddict token.
/// </summary> /// </summary>
public class OpenIddictToken<TKey> where TKey : IEquatable<TKey> { public class OpenIddictToken<TKey> where TKey : IEquatable<TKey> {
/// <summary>
/// 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.
/// </summary>
public virtual TKey AuthorizationId { get; set; }
/// <summary> /// <summary>
/// Gets or sets the unique identifier /// Gets or sets the unique identifier
/// associated with the current token. /// associated with the current token.
@ -38,11 +31,5 @@ namespace OpenIddict {
/// Gets or sets the type of the current token. /// Gets or sets the type of the current token.
/// </summary> /// </summary>
public virtual string Type { get; set; } public virtual string Type { get; set; }
/// <summary>
/// Gets or sets the identifier of the user attached with the current token.
/// This property is null if the token represents a client application.
/// </summary>
public virtual TKey UserId { get; set; }
} }
} }

10
src/OpenIddict.EntityFramework/OpenIddictContext.cs

@ -127,6 +127,10 @@ namespace OpenIddict {
protected override void OnModelCreating(ModelBuilder builder) { protected override void OnModelCreating(ModelBuilder builder) {
base.OnModelCreating(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. // Configure the TApplication entity.
builder.Entity<TApplication>(entity => { builder.Entity<TApplication>(entity => {
entity.HasKey(application => application.Id); entity.HasKey(application => application.Id);
@ -140,7 +144,7 @@ namespace OpenIddict {
entity.HasMany(authorization => authorization.Tokens) entity.HasMany(authorization => authorization.Tokens)
.WithOne() .WithOne()
.HasForeignKey(token => token.AuthorizationId) .HasForeignKey("AuthorizationId")
.IsRequired(required: false); .IsRequired(required: false);
entity.ToTable("OpenIddictAuthorizations"); entity.ToTable("OpenIddictAuthorizations");
@ -164,12 +168,12 @@ namespace OpenIddict {
builder.Entity<TUser>(entity => { builder.Entity<TUser>(entity => {
entity.HasMany(user => user.Authorizations) entity.HasMany(user => user.Authorizations)
.WithOne() .WithOne()
.HasForeignKey(authorization => authorization.UserId) .HasForeignKey("UserId")
.IsRequired(required: false); .IsRequired(required: false);
entity.HasMany(user => user.Tokens) entity.HasMany(user => user.Tokens)
.WithOne() .WithOne()
.HasForeignKey(token => token.UserId) .HasForeignKey("UserId")
.IsRequired(required: false); .IsRequired(required: false);
}); });
} }

Loading…
Cancel
Save