简体   繁体   English

如何首先停止实体框架代码运行“CREATE SCHEMA IF NOT EXISTS”

[英]How to Stop the Entity Framework Code First from running “CREATE SCHEMA IF NOT EXISTS”

Here is info about our technical development environment:以下是有关我们的技术开发环境的信息:

  • .NET Core 3.1 .NET 核心3.1
  • PostgreSQL 14.2, compiled by Visual C++ build 1914, 64-bit PostgreSQL 14.2,由 Visual C++ build 1914 编译,64 位
  • EntityFramework.Functions Version=1.5.0 EntityFramework.Functions 版本=1.5.0
  • Microsoft.EntityFrameworkCore.Design Version=5.0.17 Microsoft.EntityFrameworkCore.Design 版本=5.0.17
  • Microsoft.EntityFrameworkCore.Tools Version=5.0.17 Microsoft.EntityFrameworkCore.Tools 版本=5.0.17
  • Npgsql.EntityFrameworkCore.PostgreSQL Version=5.0.10 Npgsql.EntityFrameworkCore.PostgreSQL 版本=5.0.10

We are using a PostgreSQL user account that is very restrictive because we Only want said account to create typical database objects (ie tables, views, functions, procedures, etc), but we do not want said account to create database schemas.我们正在使用 PostgreSQL 用户帐户,该帐户非常受限制,因为我们只希望该帐户创建典型的数据库对象(即表、视图、函数、过程等),但我们不希望该帐户创建数据库模式。 It's just for security and make our environment more bullet-proof.这只是为了安全,让我们的环境更加防弹。

public class DatabaseContext : DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
         modelBuilder.HasDefaultSchema(Constants.SchemaName);
         base.OnModelCreating(modelBuilder);
         modelBuilder.ApplyConfiguration(new SuperMarketEntityTypeMap());
    } 

    public DbSet<SuperMarket> supermarkets { get; set; }

    public void SetCommandTimeout(int? timeout)
    {
         this.Database.SetCommandTimeout(timeout);
    }

    public override void Dispose()
    {
        base.Dispose();
    }
}

public class DatabaseContextFactory : IDesignTimeDbContextFactory<DatabaseContext>
{
     public DatabaseContext CreateDbContext(string[] args)
     {
         var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>();
         optionsBuilder.UseNpgsql(x => x.MigrationsHistoryTable(
         HistoryRepository.DefaultTableName, Constants.SchemaName));

         return new DatabaseContext(optionsBuilder.Options);
    }
}

From my PowerShell commandline, when I run an update like the following, it throws an error when trying to "CREATE SCHEMA IF NOT EXISTS" because the we have not granted the PostgreSQL user account the privileges to create database schemas.在我的 PowerShell 命令行中,当我运行如下更新时,它会在尝试“如果不存在时创建模式”时抛出错误,因为我们没有授予 PostgreSQL 用户帐户创建数据库模式的权限。 We only want said user account to create typical database objects (ie tables, views, functions, procedures, etc):我们只希望所述用户帐户创建典型的数据库对象(即表、视图、函数、过程等):

dotnet ef database update –connection “Blah Blah Blah Connection String Blah Blah Blah” 

Build started... 
Build succeeded.

Failed executing DbCommand (115ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE SCHEMA IF NOT EXISTS supermarkets_schema; CREATE TABLE sendgrid_status."__EFMigrationsHistory" (
 "MigrationId" character varying(150) NOT NULL,
 "ProductVersion" character varying(32) NOT NULL,
 CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId") );

Could someone please post a response with code and/or commandline execution arguments and/or configuration changes that will show how I can stop the Entity Framework code-first from running "CREATE SCHEMA IF NOT EXISTS"?有人可以发布带有代码和/或命令行执行 arguments 和/或配置更改的响应,这些更改将显示我如何阻止实体框架代码优先运行“CREATE SCHEMA IF NOT EXISTS”吗?

Essentially, in the following post, the developer overrides the NpgsqlMigrationsSqlGenerator's Generate with an empty method in a customized Subclass of NpgsqlMigrationsSqlGenerator which is called MyMigrationsSqlGenerator本质上,在下面的帖子中,开发人员在 NpgsqlMigrationsSqlGenerator 的自定义子类中使用一个空方法覆盖了 NpgsqlMigrationsSqlGenerator 的生成,该子类称为 MyMigrationsSqlGenerator

( Technical Reference: https://github.com/npgsql/efcore.pg/issues/1770 ) (技术参考: https://github.com/npgsql/efcore.pg/issues/1770

---Excerpt from posting above---------------------------- ---摘自上面的帖子----------------------------

Had the same issue described here (we're mapping each application to a separate schema, with strict isolation between schemas).遇到了此处描述的相同问题(我们将每个应用程序映射到一个单独的模式,模式之间严格隔离)。 I was able to work around it thanks to the hints from Shay:感谢 Shay 的提示,我能够解决它:

 public class MyMigrationsSqlGenerator: NpgsqlMigrationsSqlGenerator { public MyMigrationsSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, INpgsqlOptions npgsqlOptions): base(dependencies, npgsqlOptions) { } protected override void Generate(EnsureSchemaOperation operation, IModel model, MigrationCommandListBuilder builder) { // don't generate schema operations as they are not permitted with our current permissions setup } }

then hooked this up with:然后将其与以下内容联系起来:

 .ReplaceService<IMigrationsSqlGenerator, NpgsqlMigrationsSqlGenerator, MyMigrationsSqlGenerator>()

---Excerpt from posting above---------------------------- ---摘自上面的帖子----------------------------

To elaborate on where the ReplaceService line of code above goes, it is placed in my IDesignTimeDbContextFactory implementation here:为了详细说明上面 ReplaceService 代码行的位置,它位于我的 IDesignTimeDbContextFactory 实现中:

 public class DatabaseContextFactory: IDesignTimeDbContextFactory<DatabaseContext> { public DatabaseContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<DatabaseContext>(); optionsBuilder.ReplaceService<IMigrationsSqlGenerator, NpgsqlMigrationsSqlGenerator, MyMigrationsSqlGenerator>().UseNpgsql( x => x.MigrationsHistoryTable( HistoryRepository.DefaultTableName, "Blah Blah Blah Database Schema Name Blah Blah")); return new DatabaseContext(optionsBuilder.Options); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法首先使用Npgsql和Entity Framework代码在PostreSQL中创建数据库 - Unable to create database in PostreSQL using Npgsql and Entity Framework code first 如何从 PostgreSQL 模式构建实体框架 model? - How to build an Entity Framework model from a PostgreSQL schema? 如何使用带有实体框架代码优先的 Postgresql 的串行字段 - How to use a serial field with Postgresql with Entity Framework Code First 实体框架代码首先在生产中使用mysql - Entity Framework code first with mysql in Production 实体框架+ PostgreSQL代码优先 - Entity Framework + PostgreSQL code-first 如何从Entity Framework 6(数据库优先)和Npgsql执行存储过程? - How to execute stored procedure from Entity Framework 6 (database first) and Npgsql? SQL Server和Postgres上的实体框架代码优先 - Entity Framework code-first on SQL Server and Postgres 使用实体框架的代码优先在PostgreSQL中存储图像 - Using Entity Framework's Code First to store an image in PostgreSQL PostgreSQL 的 Entity Framework Core 代码第一个默认值 - Entity Framework Core code first default values for PostgreSQL 如何从 Flask 应用程序创建新模式 - How to create a new schema from a flask application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM