使用Azure Active Directory应用程序对SQL数据库进行身份验证时,实体框架代码优先迁移无法正常工作

时间:2019-05-02 02:18:48

标签: entity-framework azure azure-active-directory

我首先将Entity Framework代码与db迁移一起用于Azure SQL Server。最近,我尝试通过本文https://docs.microsoft.com/en-us/azure/sql-database/sql-database-aad-authentication-configure使用Azure Active Directory身份验证。我正在将SQL连接传递给使用应用程序访问令牌构造的实体框架。一切正常,但数据库迁移正常。迁移数据库时,出现以下错误。对此有任何帮助。

System.InvalidOperationException   HResult = 0x80131509   Message =此操作需要连接到“ master”数据库。由于原始数据库连接已打开并且凭据已从连接字符串中删除,因此无法创建与“ master”数据库的连接。提供未打开的连接。   源= EntityFramework.SqlServer   堆栈跟踪:    在System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection处(DbConnection sqlConnection,Action 1 act) at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable 1 commandTimeout,DbConnection sqlConnection,字符串createDatabaseScript)    在System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase处(DbConnection连接,可空1 commandTimeout, StoreItemCollection storeItemCollection) at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable 1 commandTimeout,StoreItemCollection storeItemCollection)    在System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()    在System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection连接)    在System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(操作mustSucceedToKeepDatabase)    在System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)    在System.Data.Entity.MigrateDatabaseToLatestVersion 2.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf 1.b__e()    在System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action动作)    在System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()    在System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)    在System.Data.Entity.Internal.RetryAction 1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action 1操作中)    在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()    在System.Data.Entity.Internal.InternalContext.Initialize()    在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(TypeEntityType)    在System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()    在System.Data.Entity.Internal.Linq.InternalSet 1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet 1.Add(对象实体)处    在System.Data.Entity.DbSet`1.Add(TEntity实体)

内部异常1: SqlException:用户“”登录失败。

2 个答案:

答案 0 :(得分:0)

在浏览此documentation时,我认为修改连接字符串步骤需要执行两次,一次用于应用程序数据库,另一次用于访问master数据库,因为您不能简单地浏览到master而不进行重新认证或未授予使用的凭据访问主数据库的权限。

我认为在分配令牌,禁用令牌以及应用程序按预期开始运行之后,包含以下行是关键。

Database.SetInitializer<MyDatabaseContext>(null);

答案 1 :(得分:0)

我试图在EF6中做同样的事情,发现用当前的EF6库是不可能的。 问题是DbMigrator总是尝试使用连接字符串打开SQL连接,并且不允许注入打开的SQL连接。 https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.migrations.dbmigrator?view=entity-framework-6.2.0