5

我正在使用 Entity Framework 4 和 CTP 4,到目前为止我真的很喜欢它。当我使用 Windows 身份验证连接到数据库时,我可以让它正常工作,但是当我切换到 SQL 身份验证时,它会失败并显示以下消息。我给用户一个系统管理员角色,它的默认数据库是主数据库。

错误:

此操作需要连接到“主”数据库。无法创建到“主”数据库的连接,因为原始数据库连接已打开并且凭据已从连接字符串中删除。提供一个未打开的连接。

有任何想法吗?

...更多错误细节:

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

2 回答 2

2

这对我有用。

   Database.SetInitializer<YourDbContext>(null);

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

于 2010-09-07T14:22:38.400 回答
1

关于以下异常:
System.InvalidOperationException:此操作需要连接到“主”数据库。无法创建到“主”数据库的连接,因为原始数据库连接已打开并且凭据已从连接字符串中删除。提供一个未打开的连接。System.Data.SqlClient.SqlException:用户“sa”登录失败

Entity Framework 4.1 Update 1 包含一个错误修复,以消除在使用 SQL 身份验证时在连接字符串中指定“Persist Security Info=True”的需要。
http://www.microsoft.com/download/en/details.aspx?id=26825

于 2011-08-05T07:06:35.427 回答