【问题标题】:Entity Framework - Empty Lists实体框架 - 空列表
【发布时间】:2017-05-18 05:27:48
【问题描述】:

你好,

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

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

here您可以使用快速入门 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 身份验证流程)但列表是空的...

对此有什么想法吗?

干杯

【问题讨论】:

  • 你在使用 EF Core 吗?如果是,您需要预先加载相关的集合(使用Include / ThenInclude)。见Loading Related Data。
  • 完美运行!谢谢各位

标签: c# entity-framework asp.net-core identity identityserver4


【解决方案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);

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

【讨论】:

  • 就像一个魅力!不知道必须使用“包含”来加载依赖项。谢谢你,好心的先生!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多