Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework, code first : Avoid unwanted connections to the azure master database

When EF tries to connect to Azure sql database (perhaps when trying to open a connection to the database, not sure) it attempts to connect/login to the master database, the question is why?

The user on the connection string is not allowed to connect to the master database, from what I can see logged, errors appear from EF trying unsuccessfully to login the master database with the user, but functionally the application works correctly (The app does not require access to the master db to do anything).

From my research it seems most similar to this issue, but that's has been closed as not reproducible and am not sure whether this occurs when trying to obtain ProviderManifestToken.

So given a database user with the right permissions on the database that it needs, and the connection string reflecting this, why does EF attempt to connect to the master database?

Sample connection string for the context

Server=tcp:MyServer;Database=MyDb;User ID=MyDatabaseUser;Password=*****;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;TrustServerCertificate=False;

Log error message below:

15-05-07 04:06:16.404 System.Data.SqlClient.SqlException (0x80131904): The server principal "MyDatabaseUser" is not able to access the database "master" under the current security context.
Database 'master' on server 'MyServer' is not currently available.  Please retry the connection later.  If the problem persists, contact customer support, and provide them the session tracing ID of '69955F47-7746-41560-A03C-807FB9A56478A'.
Login failed for user 'MyDatabaseUser'.
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
ClientConnectionId:5a5e2892-0d1f-471c-a1b7-5f9e5705655f1
ErrorCode: 916
ErrorCode: 40613
ErrorCode: 18456
like image 942
Mayoweezy Avatar asked May 08 '15 12:05

Mayoweezy


People also ask

How to use SQL Azure with Entity Framework?

To use SQL Azure with your Entity Framework, first create an SQL database first in your Azure account. If you want learn more about SQL Azure then follow the following links: So, let’s make an SQL database. Here are the steps, Go to your Azure account find New, Data + Storage, then click SQL Databases.

How to create an Entity Framework data model in Visual Studio?

We will use the Entity Framework Tools for Visual Studio to help us generate some initial code to map to the database. These tools are just generating code that you could also type by hand if you prefer. Project -> Add New Item… Select Data from the left menu and then ADO.NET Entity Data Model

How do I use SQL Server with Entity Framework dbcontext?

By default, a DbContext doesn’t know what storage mechanism to use. To use SQL Server, override the OnConfiguring method of the dbContext, and use the UseSqlServer method from the EntityFrameworkCore.SqlServer NuGet package: In the previous steps, we’ve created an application that wants to store data in a database.

How do I deploy a code first database to Azure web?

To deploy a Code First database to an Azure Web Site, you can use the Execute Code First Migrations check box in the Publish Web wizard:


1 Answers

Entity Framework is trying to connect to the master database because Code First attempts to create the database for the project if it doesn't exist. This can only be done from master.

You can still use Code First and work against an existing database, but you need to change the settings at least so it won't try to create the database. You can set it for updates, that's fine. My personal preference is to turn all database management off, and handle it myself.

This is done by creating a static overload constructor on your DbContext:

public static MyContext() : base(null) {}

This link discusses the different database initializers that are available. The above static constructor creates no initializer. Here is another link that discusses the same initializers, as well as creating custom ones.

like image 102
krillgar Avatar answered Oct 29 '22 17:10

krillgar