在ASP.NET Core 7.0中使用Postgresql DbContext添加身份脚手架

k3bvogb1  于 2023-08-04  发布在  PostgreSQL
关注(0)|答案(2)|浏览(100)

我已经将我一个月前的项目转换为使用PostgreSQL而不是SQL Server,我测试了它,它正确地应用了身份迁移并创建了表。当使用Google OAuth登录时,它将新记录写入Google帐户的数据库。接下来,我想改变登录和其他身份页面的UI,所以我去添加身份页面的脚手架,但当对话框弹出的页面,你想覆盖,它只显示一个禁用的“SQL Server”作为数据库提供商。我在网上看到过其他帖子,他们在那里列出了适当的提供程序,如MySql Lite。我已经删除了SQL Server的包,并添加了:

  • Npgsql.EntityFrameworkCore.PostgreSQL
  • Npgsql.EntityFrameworkCore.PostgreSQL.Design
    Program.cs包含
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") 
     ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
            options.UseNpgsql(connectionString));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

// then lower
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

字符串

ApplicationDbContext.cs使用预生成,然后添加创建部分和OnConfiguring

public class ApplicationDbContext : IdentityDbContext
    {
        private readonly IConfiguration _configuration;
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
            [FromServices] IConfiguration configuration)
            : base(options)
        {
            _configuration = configuration;
            try
            {
                // Create the database if it doesn't exist
                var databaseCreator = Database.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
                if (databaseCreator != null)
                {
                    if (!databaseCreator.CanConnect()) { databaseCreator.Create(); }
                    if (!databaseCreator.HasTables()) { databaseCreator.CreateTables(); }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            // from environment vars
            //var dbHost = Environment.GetEnvironmentVariable("DB_HOST");
            //var dbName = Environment.GetEnvironmentVariable("DB_NAME");
            //var dbPassword = Environment.GetEnvironmentVariable("DB_SA_PASSWORD");
            //var dbUsername = Environment.GetEnvironmentVariable("DB_USERNAME");
            //var connectionString = $"Username={dbUsername};Password={dbPassword};Server={dbHost};Database={dbName};Integrated Security=true;Pooling=true;";

            //from configuration
            var connectionString = _configuration.GetConnectionString("DefaultConnection")
              ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
            optionsBuilder.UseNpgsql(connectionString);
        }
    }

appsetings.json

"ConnectionStrings": {
    "DefaultConnection": "Provider=PostgreSQL OLE DB;Driver={PostgreSQL};User ID=postgres;Password=*******;Host=**********;Database=*********"
  }


我添加了 ProviderDriver 到一个工作连接字符串,试图在不受任何影响的情况下解决我的问题。
然而,当我添加脚手架时,对话框看起来像这样:
x1c 0d1x的数据
当我点击添加时,我得到:



我已经尝试清除Package Manager存储/缓存,但问题仍然存在。



我不知道如何让项目在转换为另一个数据库提供程序后添加脚手架。
我在我的项目中搜索了任何对SQL Server的引用,但没有,这可能是它变灰的原因,但没有填充我的提供程序。
Visual Studio Community 2022版本17.5.5

pcww981p

pcww981p1#

我创建了一个丢弃项目,并创建了一个本地的git repo,然后在添加脚手架后做了一个diff来查看变化。我可以看到它改变了我在项目中的一些相关软件包的版本:


的数据
之后,我将新页面和cs代码(位于同一位置)复制到活动项目中的相应文件夹中。我运行了它,并能够看到这些新页面呈现的变化。
以下是其中的一些变化:



我没有这个问题的真正答案,但至少这是一个解决方案。也许从升级软件包版本开始就可以解决这个问题。

zd287kbt

zd287kbt2#

我遇到了同样的问题,并使用dotnet CLI来搭建。这是一个简单的过程。你可以在这里阅读更多关于它:
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-7.0&tabs=netcore-cli#scaffold-identity-into-a-razor-project-with-authorization
但是,它仍然要求你在依赖项列表中有Microsoft.EntityFrameworkCore.SqlServer,但是程序可以与PostgreSQL一起工作。否则,CLI就不允许搭建。

相关问题