【问题标题】:IdentityServer4 Sample with ASP Identity with real SQL Server带有真实 SQL Server 的 ASP 身份的 IdentityServer4 示例
【发布时间】:2020-08-18 07:30:32
【问题描述】:

我一直在努力让最终的 SAMPLE(ASP.Net、EF Core、SQL)与真正的 SQL Server 一起工作。我能找到的每个样本都没有使用真正的 SQL,他们总是选择内存数据存储

我更改了连接字符串

"Data Source=.;Initial Catalog=IS4;Integrated Security=True;"

跑了

dotnet ef database update -c ApplicationDbContext

这为我创建了一个包含 25 个表的 SQL 数据库。

我调整了 Startup.cs 来改变

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(connectionString));

和 b.UseSqlite 到 b.UseSqlServer

            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = b =>
                    b.UseSqlServer(connectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            // this adds the operational data from DB (codes, tokens, consents)
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = b =>
                    b.UseSqlServer(connectionString,
                        sql => sql.MigrationsAssembly(migrationsAssembly));

                // this enables automatic token cleanup. this is optional.
                options.EnableTokenCleanup = true;
                // options.TokenCleanupInterval = 15;
            });

我在命令行上使用“/seed”运行服务器,但种子功能不起作用

首先它抱怨 CLIENT 在调用 SaveChanges() 时不能有 NULL ID。如果我更改代码以添加 ID

        if (!context.Clients.Any())
        {
            Console.WriteLine("Clients being populated");
            int i = 1;
            foreach (var client in Config.GetClients().ToList())
            {
                var x = client.ToEntity();
                x.Id = i++;
                context.Clients.Add(x);
            }
            context.SaveChanges();
        }
        else
        {
            Console.WriteLine("Clients already populated");
        }

然后我得到

"Cannot insert the value NULL into column 'Id', table 'IS4.dbo.ClientGrantTypes".

当我观看视频时,它说只需更改连接字符串即可将其从 SQLite 迁移到完整 SQL,鉴于我所做的所有其他更改,这显然不是真的,所以我必须做(或错过)一些事情否则。

有什么想法吗?

【问题讨论】:

  • 您是说将所有 ConfigurationDbContext 表和 PersistedGrantDbContext 表添加到您自己的 ApplicationDbContext 中吗?
  • 我按照示例说明进行操作,迁移创建了表。只有一个默认数据库连接,迁移只有“ApplicationDbContextModelSnapshots”。

标签: sql-server asp.net-core asp.net-identity entity-framework-core identityserver4


【解决方案1】:

会不会是所有具有“Id INT”列的表都应该是 IDENTITY 列,而它们不是!

我检查了迁移代码,它有

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ApiResources",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Description = table.Column<string>(maxLength: 1000, nullable: true),
                    DisplayName = table.Column<string>(maxLength: 200, nullable: true),

我猜

.Annotation("Sqlite:Autoincrement", true),

不适用于完整的 SQL,因此所有表都需要标识属性设置。

有趣的是,如果您运行其他模板来添加 AdminUI

dotnet new is4admin

好像加了几个SQL脚本

CREATE TABLE "Clients" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_Clients" PRIMARY KEY AUTOINCREMENT,
    "AbsoluteRefreshTokenLifetime" INTEGER NOT NULL,
    "AccessTokenLifetime" INTEGER NOT NULL,

这确实使它们成为标识列。

【讨论】:

  • 你可能已经标记了这个,但它似乎可以解决问题
【解决方案2】:

我今天遇到了这个问题,在网上搜索了几次,偶然发现了这个https://entityframeworkcore.com/knowledge-base/46587067/ef-core---do-sqlserver-migrations-apply-to-sqlite-

链接指出切换迁移类UP方法后的注解部分

Id = table.Column(nullable: false)

来自

.Annotation("Sqlite:Autoincrement", true);

.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)

你需要导入

使用 Microsoft.EntityFrameworkCore.Metadata;

然后你构建,迁移就成功了。

【讨论】:

  • 创建 .上述解决方案对我有用。谢谢
【解决方案3】:

为了解决这个特殊问题,我使用了 SSMS。

  1. 右键单击表格
  2. 选择要删除和创建的脚本
  3. 在 NOT NULL 之后添加 IDENTITY
  4. 执行

不管你是对的,它是在 sql 文件和迁移中使用 sqlite 注释。

要完全解决此问题,您需要创建所有 3 个必要数据库上下文的实现:身份、持久授权和配置。

这也需要为每个上下文实现设计时工厂。

然后您可以在包管理器控制台中为每个上下文运行 add-migration,然后运行更新数据库,或在播种时使用 migrate 功能运行应用程序。

回顾一下:

  1. 为 3 个数据库上下文创建实现
  2. 为这些数据库上下文创建设计时工厂实现
  3. 添加迁移
  4. 使用这些迁移更新数据库

【讨论】:

    猜你喜欢
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2017-12-06
    • 2014-07-16
    相关资源
    最近更新 更多