【发布时间】: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