2010-09-06 64 views
5

我正在使用Entity Framework 4和CTP 4,到目前爲止我非常喜歡它。當我使用Windows身份驗證連接到數據庫時,我可以正常工作,但當切換到SQL身份驗證時,它會失敗,並顯示以下消息。我給了用戶一個sysadmin角色,它的默認數據庫是master。代碼優先實體框架4:通過SQL身份驗證失敗

錯誤:

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.

任何想法?

...更多錯誤詳細信息:

[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 

回答

2

這爲我工作。

Database.SetInitializer<YourDbContext>(null); 

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

+0

只是去顯示'EF'這個EF實際上是如何。 「到目前爲止我真的很喜歡它」讓我感到很恐懼。 – 2010-09-07 14:27:14

+0

除非在模型更改時確實要放下,否則始終放下...此(空)調用程序始終將其設置爲CreateIfNotExists。 – bc3tech 2014-06-21 21:45:20

1

在問候以下異常:
System.InvalidOperationException:此操作需要在 '主' 數據庫的連接。無法創建與「主」數據庫的連接,因爲原始數據庫連接已打開且憑據已從連接字符串中刪除。提供未打開的連接。 System.Data.SqlClient.SqlException:用戶'sa'登錄失敗

實體框架4.1 Update 1包含一個錯誤修復程序,用於在使用SQL身份驗證時刪除在連接字符串中指定「Persist Security Info = True」的需要。
http://www.microsoft.com/download/en/details.aspx?id=26825