0

从 VS Community 2017 MVC 5 项目运行 dacpac 时出现此错误。

"Internal Error. The database platform service with type Microsoft.Data.Tools.Schema.Sql.Sql140DatabaseSchemaProvider is not valid. You must make sure the service is loaded, or you must provide the full type name of a valid database platform service."

编码

            var dacpacName = "setup.dacpac";                 
            var dacpacPath = Path.Combine(Server.MapPath("~/assets/dacpac"), dacpacName);
            var dp = DacPackage.Load(dacpacPath);
            var dbDeployOptions = new DacDeployOptions
            {
                BlockOnPossibleDataLoss = false,
                ScriptDatabaseOptions = false,
                GenerateSmartDefaults = true,
                CreateNewDatabase = true
            }; 
            var dbServices = new DacServices(setupDbConn.ConnectionString);
            dbServices.Deploy(dp, newDatabaseName, true, dbDeployOptions);

错误发生在此代码之后:

var dbServices = new DacServices(setupDbConn.ConnectionString);

我的 SqlExpress 是 2017 年。我也从 SqlExpress 2017 重新生成了 dacpac。当我在 SqlExpress 中使用 dacpac 时,它可以正常工作。

我已经在谷歌上搜索了几个小时,但似乎找不到正确的答案。我认为这是某种兼容性问题,但无法弄清楚如何解决该错误。

希望有人经历过这个,可以帮助我解决这个问题。

4

1 回答 1

0

我已经尝试了我能想到的一切。我安装了不同版本的 dacfx。还尝试从 2008 年开始生成 dacpac。一切都没有运气。

然后我采取了其他方式而不是使用 dacpac。我已经生成了一个脚本来重新创建所有表并运行它。

Server myServer = new Server(serverName);

//Using windows authentication
bool integratedSecurity = Convert.ToBoolean(ConfigurationManager.AppSettings["integratedSecurity"]);    
myServer.ConnectionContext.LoginSecure = integratedSecurity;        
myServer.ConnectionContext.Connect();    

// check that database doesn't already exists
if (myServer.Databases.Contains(newDatabaseName))
{
    return new HttpStatusCodeResult(400, "Database has already been created");
}

//Define a Database object variable by supplying the server and the database name arguments in the constructor.   
Database db;
db = new Database(myServer, newDatabaseName);

//Create the database on the instance of SQL Server.   
db.Create();

string dbscript = System.IO.File.ReadAllText(Server.MapPath("~/assets/dbscript/createAllTables.sql"));    
myServer.Databases[newDatabaseName].ExecuteNonQuery(dbscript);
myServer.ConnectionContext.Disconnect();
于 2019-02-07T14:47:39.393 回答