对DacServices.Deploy
的此调用对SQL Server LocalDB 2014非常有用,但在安装SQL Server LocalDB 2016时失败:
string dacConnectionString = $"Server=(localdb)\\mssqllocaldb; Integrated Security=true; database={DatabaseName}";
var dacServices = new DacServices(dacConnectionString);
dacServices.Message += (sender, args) => Console.WriteLine($"{args.Message.Prefix}: {args.Message.Message}"); // Log dacpac deploy messages
dacServices.Deploy(LoadDacPac(), DatabaseName, true, new DacDeployOptions()
{
BlockOnPossibleDataLoss = false
});
DacServices.Deploy
针对LocalDB 2016抛出的异常是:
Microsoft.SqlServer.Dac.DacServicesException was unhandled by user code
HResult=-2146233088
Message=Could not deploy package.
Source=Microsoft.SqlServer.Dac
StackTrace:
at Microsoft.SqlServer.Dac.DeployOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.OperationExtension.Execute(IOperation operation, DacLoggingContext loggingContext, CancellationToken cancellationToken)
at Microsoft.SqlServer.Dac.DacServices.InternalDeploy(IPackageSource packageSource, Boolean isDacpac, String targetDatabaseName, DacDeployOptions options, CancellationToken cancellationToken, DacLoggingContext loggingContext)
at Microsoft.SqlServer.Dac.DacServices.Deploy(DacPackage package, String targetDatabaseName, Boolean upgradeExisting, DacDeployOptions options, Nullable`1 cancellationToken)
at Tv.Base.Test.Database.TestSqlLocalDb.CreateOrUpdateDatabaseIfNeeded(Boolean force) in D:\BuildAgent-02\work\6ec37398501798d0\src\Base.Test.Database\TestSqlLocalDb.cs:line 173
at Tv.Services.Inventory.DataAccess.Tests.InventoryDatabaseFixture..ctor() in C:\src\tv\services\inventory\test\DataAccess.Tests\InventoryDatabaseFixture.cs:line 40
InnerException:
HResult=-2146233088
Message=Unable to connect to target server.
Source=Microsoft.Data.Tools.Schema.Sql
StackTrace:
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentEndpointServer.OnInit(ErrorManager errors, String targetDBName)
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeployment..ctor(SqlDeploymentConstructor constructor)
at Microsoft.Data.Tools.Schema.Sql.Deployment.SqlDeploymentConstructor.ConstructServiceImplementation()
at Microsoft.SqlServer.Dac.DacServices.CreatePackageToDatabaseDeployment(String connectionString, IPackageSource packageSource, String targetDatabaseName, DacDeployOptions options, ErrorManager errorManager)
at Microsoft.SqlServer.Dac.DeployOperation.<>c__DisplayClass3.<>c__DisplayClass5.<CreatePlanInitializationOperation>b__1()
at Microsoft.Data.Tools.Schema.Sql.Dac.OperationLogger.Capture(Action action)
at Microsoft.SqlServer.Dac.DeployOperation.<>c__DisplayClass3.<CreatePlanInitializationOperation>b__0(Object operation, CancellationToken token)
at Microsoft.SqlServer.Dac.Operation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.ReportMessageOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.OperationExtension.CompositeOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.OperationExtension.CompositeOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
at Microsoft.SqlServer.Dac.DeployOperation.Microsoft.SqlServer.Dac.IOperation.Run(OperationContext context)
“无法连接”错误似乎不正确/可能会掩盖真正的错误,因为指定的连接字符串允许我使用SqlConnection
连接到数据库,因为我可以将此dacpac部署到SQL Server使用命令行的LocalDB 2016:
sqlpackage "/Action:publish" "/SourceFile:MyDatabase.dacpac" "/TargetConnectionString:Server=(localdb)\mssqllocaldb;Database=MyDatabase;Integrated Security=true"
有关我的设置的更多信息:
> sqllocaldb info mssqllocaldb
Name: MSSQLLocalDB
Version: 13.0.1601.5
Shared name:
Owner: DOMAIN\user
Auto-create: Yes
State: Running
Last start time: 7/1/2016 5:09:43 PM
Instance pipe name: np:\\.\pipe\LOCALDB#C1DD8548\tsql\query
> sqllocaldb v
Microsoft SQL Server 2014 (12.0.2000.8)
Microsoft SQL Server 2016 (13.0.1601.5)
正在使用的Microsoft.SqlServer.Dac
程序集来自此NuGet包:
https://www.nuget.org/packages/Microsoft.SqlServer.Dac
答案 0 :(得分:8)
对此的修复确实是为了更新我们正在使用的Microsoft.SqlServer.Dac
程序集的版本 - 我发现我应该在看到@ kevin-cunnane的建议之前不久尝试一下
有一些因素使得这一点不那么明显,这也就是SO的原因:
Microsoft.SqlServer.Dac
和相关程序集的NuGet包。其中一些不是由Microsoft维护的,包括我正在使用的那个(Microsoft.SqlServer.Dac)。微软官方版本直到2016年6月才在NuGet.org上发布,它没有最明显的NuGet id(Microsoft.SqlServer.DacFx.x64)。因此,运行update-package Microsoft.SqlServer.Dac
没有达到预期的效果。 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\130\Microsoft.SqlServer.Dac.dll
),但它们并未安装在GAC中,或者很容易找到。 有效的解决方案是
# Remove the old NuGet dependencies
uninstall-package Microsoft.SqlServer.Dac
# Install the new Dac NuGet package
Install-Package Microsoft.SqlServer.DacFx.x64
对Dac团队的要求,如果你碰巧看到这个:
(顺便说一下,尽管有困难,Dac / SSDT AWESOME 。我还没有看到任何可比的开发工具适用于任何竞争关系数据库。)