0

I am building an ASP.NET, code-first web app. I am using a company owned database, and have crud permissions for tables within one database on this server. I do not believe I have "Create Database" permissions. I am running into issues publishing my web app. I can run everything locally, but when I publish the code on the company server I get errors like:

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

This error makes me believe I have an issue with my connection string. When using Unit Testing, I get the error:

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'.

I recently changed my context constructor from:

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

to:

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

And when I ran the solution locally, also got the "CREATE DATABASE permission denied in database 'master'" error.

For the record, my connection string is:

<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>

My question is:

Why do I need Create Database Permissions in order for this all to work? And if that isn't the case, why do I keep getting this error? (No where in my code do I say create DB, nor should / can I).

Secondly, any suggestions as to fixing my connection string? I got this connectionString from database -> properties -> Connection String (so I feel like it's correct).

Thanks for all help / tips / explanations.

Using:

  • ASP.NET / Entity Framework / C#
  • Visual Studio 2015
  • SQL Server Management Studio 2014
swallis1
  • 99
  • 1
  • 13
  • Can you create database on that server manually? I mean by login with that provided credential directly to sql server on SQL management studio. – Amir Jan 23 '17 at 17:48
  • I'm not sure if I can, but I'm not suppose to. It is a very large corporation and I'm not suppose to be messing with things like that. – swallis1 Jan 23 '17 at 17:50
  • It will be better if you could create any test database and delete it later and find out you have permission or not. – Amir Jan 23 '17 at 17:52
  • Ok - I tried to create a new database and got "CREATE DATABASE permission denied in database 'master'. (Microsoft SQL Server, Error: 262)". – swallis1 Jan 23 '17 at 17:57
  • That means you have permission issue. right? You may try again after you get the permission from database administrator – Amir Jan 23 '17 at 18:01
  • I am not attempting to create a database. I will not be able to get create database permissions. My question is why am I being prompted with this error when I am not trying to create a db? – swallis1 Jan 23 '17 at 19:28

2 Answers2

0

There is nothing much you can do here except getting the privileges for your application to do such activities on db server which is very unlikely to happen in large organizations.

The best solution to this is not to include database creation operation as part of web application publishing process.

Surely organization's policy will not allow application to run with master db privileges.

Leave database creation activity to the DBAs who are managing the DB Servers and the databases. They will have necessary access and permissions to perform such operations. They will create database and run the script to make your database ready to use by the application.

Create a script which will create the entire database structure including tables, views, stored procs, functions, keys, indexes etc and raise a ticket or RFC or follow whatever process required to get the database created with proper structure on the target server. Let your application deal with the data only without worrying about database creation and updates.

That's how the applications in production environment works and that's how the processes are implemented and followed with very rare exceptions to it.

Chetan
  • 6,711
  • 3
  • 22
  • 32
  • I do not attempt to create a database in my code. I create tables, which I'm allowed to do in the server. I am able to create tables, create stored procedures, update tables, drop tables, etc. I have done this successfully within SQL Server Management Studio & in the code - locally. I run into issues when I try to access the db from the published dev environment - even just a select statement crashes the code. – swallis1 Jan 23 '17 at 19:27
  • Local db server and enterprise db server are not the same. The error says the "create database permission denied." That means EF is trying to create database first because it does not exist on the dev server. So you first need to get the database created there by DBA and also request for schema owner permission for the user which is part of your connection string – Chetan Jan 23 '17 at 23:17
  • But arrangement will work only on dev environment. For staging and production environment db owner role not be permitted at all. Your application will be permitted to do only data select, update, delete, run stored proc or something else which is needed for the application to rum properly. Schema change bu application will not be allowed at all for sure in stage and prod environment. – Chetan Jan 23 '17 at 23:38
  • However, I have not been using the local db - only the dev server db. I'm using an existing database on the server, I created the schema, and I've used EF to create all my tables on the server dev db. My application only calls for crud operations on tables. – swallis1 Jan 24 '17 at 17:28
  • Then the real question is why EF is trying to create new database. Any reason for changing the constructor of Context? Can you also share an example value of [server_Name].[full_company_server_file_path]? – Chetan Jan 24 '17 at 20:25
0

SOLUTION

For anyone curious, in the process of publishing (native Visual Studio action) the connection string was being garbled to:

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

I edited the published connection string to my real connection string and that fixed the problem.

swallis1
  • 99
  • 1
  • 13