C# Entity Framework 在 MSSQL 服务器拒绝视图任何数据库时出现错误。

huangapple go评论61阅读模式
英文:

C# Entity Framework error when MSSQL server has View any database = denied

问题

我们在C# .NET Framework 4.8应用程序中使用MSSQL,使用Entity Framework进行与数据库相关的操作。
但是在我们的生产环境中,SQL服务器具有Securable: View any database on Deny权限。

应用程序的数据库存在,但Entity Framework无法看到数据库并尝试创建它,导致出现“CREATE DATABASE权限在数据库'master'中被拒绝”的错误。

我在Application_Start()中使用CreateDatabaseIfNotExists和MigrateDatabaseToLatestVersion。
现在问题(我认为)出现在CreateDatabaseIfNotExists上。

对于第一次运行,我们为数据库用户提供足够的权限来创建和填充数据库,这没有问题。

但在初始设置之后,我们撤销了这些权限,问题就开始出现了。

它尝试创建数据库,但它已经存在。

我希望有一种方法可以同时实现自动数据库创建/迁移和拒绝查看任何数据库的安全性。

有人知道如何解决这个问题吗?

是否有一种选项我可以启用以阻止这种行为?

英文:

We use MSSQL for our C# .NET Framework 4.8 Application using Entity Framework for database related activities.
But on our production environment the SQL server has the Securable: View any database on Deny.

The database for the application exists but Entity Framework cannot see the database and tries to create it, this results in the CREATE DATABASE permission denied in database 'master' error.

I am using CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion in my Application_Start().
Now the issue (I think) lies with CreateDatabaseIfNotExists.

For the first run we give the db user enough rights to create and fill the database, it does this without problem.

But after the initial setup we remove those rights and the issue starts.

It tries to create the database, But it already exists.

And I am hoping there is a way to have both Automatic database creation/migration, and the View any database on deny securable.

Does anyone have a idea on how to solve this issue?

Is there some sort of option I could enable to stop this behaviour?

答案1

得分: 1

只需要翻译的部分:

You should "wire in" IHostingEnvironment and make sure you run

CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion

only in certain environments.

===========

For DotNet-Core (NON asp.net-core) apps.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.internal.hostingenvironment?view=dotnet-plat-ext-7.0

for asp.net-core.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iwebhostenvironment?view=aspnetcore-6.0

....

Then you will use (probably an existing)

"Is" method:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.hostingenvironmentextensions.isdevelopment?view=aspnetcore-7.0

IsDevelopment
IsProduction
IsStaging

or you have the ability to "add your own environment".. with

IsEnvironment(string)

I would NEVER leave to "auto-voodoo" what might happen to the production database.

You can see this approach:

https://stackoverflow.com/a/60399887/214977

英文:

You should "wire in" IHostingEnvironment and make sure you run

CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion

only in certain environments.

===========

For DotNet-Core (NON asp.net-core) apps.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.internal.hostingenvironment?view=dotnet-plat-ext-7.0

for asp.net-core.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iwebhostenvironment?view=aspnetcore-6.0

....

Then you will use (probably an existing)

"Is" method:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.hostingenvironmentextensions.isdevelopment?view=aspnetcore-7.0

IsDevelopment
IsProduction
IsStaging

or you have the ability to "add your own environment".. with

IsEnvironment(string)

I would NEVER leave to "auto-voodoo" what might happen to the production database.

You can see this approach:

https://stackoverflow.com/a/60399887/214977

huangapple
  • 本文由 发表于 2023年2月8日 21:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定