为什么我需要为 asp.net 网络应用程序创建数据库权限?

Why do i need to have create database permissions for asp.net web app?

我正在构建一个 ASP.NET,代码优先的网络应用程序。我正在使用公司拥有的数据库,并且对该服务器上一个数据库中的表具有 crud 权限。我不相信我有 "Create Database" 权限。我正在 运行 解决发布我的网络应用程序的问题。我可以在本地 运行 一切,但是当我在公司服务器上发布代码时,出现如下错误:

Format of the initialization string does not conform to specification starting at index 0. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

Source Error: 
 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 
[ArgumentException: Format of the initialization string does not conform to specification starting at index 0.]
   System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +1742
   System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +191
   System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +136
   System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +75
   System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +35
   System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +241
   System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +78
   System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +116
   System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +104
   System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +434
   System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config) +39
   System.Data.Entity.Internal.LazyInternalConnection.Initialize() +160
   System.Data.Entity.Internal.LazyInternalConnection.get_Connection() +16
   [Project].DBContext.Context..ctor() in C:\Users\[UserName]\Documents\[FilePath]\[Project]\DBContext\Context.cs:20
   [Project].Data_Manager.DataManager..ctor() in C:\Users\[UserName]\Documents\[FilePath]\[Project]\Data_Manager\DataManager.cs:15
   [Project].Controllers.HeaderController..ctor() in C:\Users\[UserName]\Documents\[FilePath]\[Project]\Controllers\HeaderController.cs:12

这个错误让我相信我的连接字符串有问题。使用单元测试时,出现错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: CREATE DATABASE permission denied in database 'master'.

我最近更改了我的上下文构造函数:

public Context() : base("[server_Name].[database_name]")
{}

至:

public Context() : base("[server_Name].[full_company_server_file_path]")
{}

而当我运行本地解决方案时,也得到了“CREATE DATABASE permission denied in database 'master'”错误。

郑重声明,我的连接字符串是:

<connectionStrings>
      <clear />
    <add name="[server_Name].[database_name]" providerName="System.Data.SqlClient" connectionString="Data Source=[server_Name].[full_company_server_file_path];Initial Catalog=[database_name];Persist Security Info=True;User ID=userid;Password=password;MultipleActiveResultSets=True;Application Name=EntityFramework" />
< /connectionStrings>

我的问题是:

为什么我需要创建数据库权限才能使这一切正常工作? 如果不是这种情况,为什么我会不断收到此错误? (在我的代码中我没有说创建数据库,我也不应该/不能)。

其次,关于修复我的连接字符串有什么建议吗?我从数据库 -> 属性 -> 连接字符串中得到了这个 connectionString(所以我觉得它是正确的)。

感谢所有帮助/提示/解释。

使用:

除了为您的应用程序获取在数据库服务器上执行此类活动的权限之外,您在这里无能为力,这在大型组织中不太可能发生。

最好的解决方案是不要将数据库创建操作作为 Web 应用程序发布过程的一部分。

组织的政策肯定不允许应用程序 运行 具有主数据库权限。

将数据库创建 activity 留给管理数据库服务器和数据库的 DBA。他们将拥有执行此类操作所需的访问权限。他们将创建数据库和 运行 脚本以使您的数据库可供应用程序使用。

创建一个脚本,该脚本将创建整个数据库结构,包括表、视图、存储过程、函数、键、索引等,并提出票证或 RFC 或遵循在目标服务器。让您的应用程序只处理数据,而不必担心数据库的创建和更新。

生产环境中的应用程序就是这样工作的,这就是流程的实施方式和遵循方式,很少有例外。

解决方案

对于任何好奇的人,在发布过程中(本机 Visual Studio 操作)连接字符串被乱码为:

connectionString="$(ReplacableToken_[server_name].[database_name]-Web.config Connection String_0)" 

我将已发布的连接字符串编辑为我的真实连接字符串并解决了问题。