2017-05-17 20 views
0

Helloes、Entity Framework - 空のリスト

私はIdentity Server 4を使用していますが、Entity Frameworkにいくつか問題があります。

私はクライアント用のカスタムCRUDを作成しようとしています。 あなたはクイックスタート8を使用することができます...私は、Entity Frameworkの(デフォルトIS4の実装)を使用して、複数のリストを取得しようとしているが、これらのリストが何らかの理由で空返され、単一のクライアント情報を取得するために

hereをしよう - 8_EntityFrameworkStorage - クライアントにアクセスしてプロパティを取得しようとします。ここで

は私の現在のコード私は

clnt.AllowedScopes 

を取得しようとしたがあるのです

Dbのコンテキスト

namespace IdentityServer4.EntityFramework.DbContexts 
{ 
    public class ConfigurationDbContext : DbContext, IConfigurationDbContext, IDisposable 
    { 
     public ConfigurationDbContext(DbContextOptions<ConfigurationDbContext> options, ConfigurationStoreOptions storeOptions); 

     public DbSet<Client> Clients { get; set; } 
     public DbSet<IdentityResource> IdentityResources { get; set; } 
     public DbSet<ApiResource> ApiResources { get; set; } 

     public Task<int> SaveChangesAsync(); 
     protected override void OnModelCreating(ModelBuilder modelBuilder); 
    } 
} 

クライアントエンティティ

namespace IdentityServer4.EntityFramework.Entities 
{ 
    public class Client 
    { 
     public Client(); 

     public bool AllowOfflineAccess { get; set; } 
     public List<ClientScope> AllowedScopes { get; set; } 
     // ... 
    } 
} 

カスタムサービスであり、空...

namespace IdentityServer.Services 
{ 
    public class ClientService 
    { 
     private readonly ConfigurationDbContext _context; 
     private readonly ILogger _logger; 

     public ClientService(ILoggerFactory loggerFactory, ConfigurationDbContext context) 
     { 
      _logger = loggerFactory.CreateLogger<ClientService>(); 
      _context = context; 
     } 

     //... 
     public ClientViewModel GetClientViewModel(int clientId) 
     { 
      var clnt = _context.Clients.FirstOrDefault(i => i.Id == clientId); 
      //todo :: return also the lists (they are coming empty) 

      var vm = new ClientViewModel(); 

      if (clnt == null) return vm; 

      vm = new ClientViewModel() 
      { 
       Id = clnt.Id, 
       ClientName = clnt.ClientName, 
       ClientId = clnt.ClientId, 
       AllowedGrantTypes = clnt.AllowedGrantTypes, 
       ClientSecrets = clnt.ClientSecrets, 
       RedirectUris = clnt.RedirectUris, 
       PostLogoutRedirectUris = clnt.PostLogoutRedirectUris, 
       AllowedScopes = clnt.AllowedScopes, 
       AllowAccessTokensViaBrowser = clnt.AllowAccessTokensViaBrowser, 
       AllowOfflineAccess = clnt.AllowOfflineAccess, 
       AlwaysIncludeUserClaimsInIdToken = clnt.AlwaysIncludeUserClaimsInIdToken, 
       AlwaysSendClientClaims = clnt.AlwaysSendClientClaims, 
       PrefixClientClaims = clnt.PrefixClientClaims, 
       RequireConsent = clnt.RequireConsent 
      }; 
      return vm; 
     } 

     //... 
    } 
} 

私は正しいクライアントを取得しています、それはスコープを持っている(私はデータベース上でそれをチェックして、ISの認証フローのためにそれを使用することができます)が、リストは空です...

上の任意の考えこの?

乾杯

+3

EFコアを使用していますか?もしそうなら、(Include'/'ThenInclude'を使って)熱心な読み込み関連のコレクションを必要とします。 [関連データの読み込み](https://docs.microsoft.com/en-us/ef/core/querying/related-data)を参照してください。 –

+0

完璧に作業しました!皆さんありがとうございます –

答えて

1

変更この

var clnt = _context.Clients.FirstOrDefault(i => i.Id == clientId);` 

この

var clnt = _context.Clients.Include(x => x.AllowedScopes) 
       .Include(x => x.OtherNavigationProperty) 
       .Include(....) 
       .FirstOrDefault(i => i.Id == clientId); 

IdentityServer4.EntityFrameworkはそれをしない方法についてhereを参照してください。

+0

は魅力的な作品です!依存関係の読み込みに「インクルード」を使用しなければならないかどうかはわかりませんでした。あなたの親切な先生に感謝! –