[英]How to Stop the Entity Framework Code First from running “CREATE SCHEMA IF NOT EXISTS”
Here is info about our technical development environment:以下是有关我们的技术开发环境的信息:
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.