sql-server - 代码优先 Entity Framework 4 : Failing with SQL Authentication

标签 sql-server entity-framework-4 ctp

我正在使用 Entity Framework 4 和 CTP 4,到目前为止我真的很喜欢它。当我使用 Windows 身份验证连接到数据库时,它可以正常工作,但当我切换到 SQL 身份验证时,它会失败并显示以下消息。我已为用户授予 sysadmin 角色,其默认数据库是 master。

错误:

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

最佳答案

这对我有用。

   Database.SetInitializer<YourDbContext>(null);

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

关于sql-server - 代码优先 Entity Framework 4 : Failing with SQL Authentication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3648617/

相关文章:

sql - 替换 SQL 中字符串的开头

sql-server - 如何在 mssqlserver 中对日期进行排序

SQL Server 2008 : How to find trailing spaces

c# - EF4 自引用关联 0..1 -> 1

c# - 如何仅通过一个线程使用 .net 异步 CTP

visual-studio-2015 - vs2015 ctp6创建新项目时出错

sql-server - T SQL For XML PATH Group By 作为属性或元素

multithreading - Entity Framework 4.3和线程

entity-framework - EF - 级联删除不起作用,无法删除对象