Entity Framework - 空列表

Entity Framework - Empty Lists

你好,

我正在使用 Identity Server 4,但在使用 Entity Framework 时遇到了一些问题。

我正在尝试为客户制作自定义 CRUD。 尝试获取单个客户端信息时,我尝试使用 Entity Framework(默认 IS4 实现)获取多个列表,但由于某种原因这些列表返回为空...

here 您可以使用 Quickstart 8 - 8_EntityFrameworkStorage - 并尝试访问客户端以获取其属性。

这是我当前的代码

数据库上下文

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; }
        // ...
    }
}

自定义服务,我正在尝试获取

clnt.AllowedScopes

但是是空的...

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 身份验证流程)但是列表是空的...

对此有什么想法吗?

干杯

将此更改为

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);

请参阅 here 了解 IdentityServer4.EntityFramework 是如何做到的。