5

I'm working with Entity Framework 4 and CTP 4 and am really liking it so far. I can get it working correctly when I'm using Windows Authentication to connect to the database but when I switch to SQL Authentication it fails with the message below. I've given the user a sysadmin role and it's default database is master.

Error:

This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.

Any ideas?

... some more error detail:

[InvalidOperationException: This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.]
   System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(SqlConnection sqlConnection, Action`1 act) +344
   System.Data.SqlClient.SqlProviderServices.DbDatabaseExists(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection) +231
   System.Data.Objects.ObjectContext.DatabaseExists() +84
   System.Data.Entity.Internal.DatabaseOperations.Exists(ObjectContext objectContext) +9
   System.Data.Entity.Infrastructure.Database.Exists() +53
   System.Data.Entity.Infrastructure.RecreateDatabaseIfModelChanges`1.InitializeDatabase(TContext context) +129
   System.Data.Entity.Infrastructure.<>c__DisplayClass2`1.<SetInitializer>b__0(DbContext c) +75
   System.Data.Entity.Infrastructure.Database.Initialize() +207
   System.Data.Entity.Internal.InternalContext.Initialize() +70
   System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext() +9
   System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType) +51
   System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) +17
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +24
   System.Data.Entity.Internal.Linq.EfInternalQuery`1.Initialize() +62
   System.Data.Entity.Internal.Linq.EfInternalQuery`1.get_Provider() +9
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +13
   System.Linq.Queryable.Select(IQueryable`1 source, Expression`1 selector) +63
Papa Burgundy
  • 6,397
  • 6
  • 42
  • 48

2 Answers2

2

This worked for me.

   Database.SetInitializer<YourDbContext>(null);

Thanks to http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/870959ef-5c06-4c3e-b9f8-454346592280

JimGoods
  • 167
  • 1
  • 9
  • Just goes to show how 'beta' this EF actually is. 'and am really liking it so far' scares me a lot. – Denis Valeev Sep 07 '10 at 14:27
  • Except if you actually DO want drop if model changes, or drop always... this (null) caller makes it set to CreateIfNotExists, always. – bc3tech Jun 21 '14 at 21:45
1

In regards to the following exception:
System.InvalidOperationException: This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection. System.Data.SqlClient.SqlException: Login failed for user 'sa'

Entity Framework 4.1 Update 1 contains a bug fix to remove the need to specify ‘Persist Security Info=True’ in the connection string when using SQL authentication.
http://www.microsoft.com/download/en/details.aspx?id=26825

Rami A.
  • 10,302
  • 4
  • 44
  • 87