SMO 数据库创建异常:"The PRIMARY filegroup must have at least one file"

SMO database creation exception: "The PRIMARY filegroup must have at least one file"

我正在尝试使用 SQL 服务器 SMO 类 创建数据库,但创建数据库的行抛出 FailedOperationException。

内部异常 (SmoException) 有消息 "The PRIMARY filegroup must have at least one file."。

但是我在代码中设置了 PRIMARY 文件组(或者至少我认为我是)并且我正在将数据文件(使用 .IsPrimaryFile=True)添加到组中。

我确定我遗漏了什么 obvious/simple。

任何帮助将不胜感激:-)...

Imports Microsoft.SqlServer.Management.Smo

Dim serverName = "(local)"
Dim databaseName = "TestNew3"

Dim sourceSrv = New Server(serverName)

Dim db As Database
db = New Database(sourceSrv, databaseName)

db.AutoCreateStatisticsEnabled = True
db.AutoUpdateStatisticsEnabled = True
db.AutoUpdateStatisticsAsync = True

Dim fileGroup = New FileGroup(db, "PRIMARY")
fileGroup.IsDefault = True
db.FileGroups.Add(fileGroup)

Dim dataFile = New DataFile(
                           fileGroup,
                           databaseName,
                           String.Format(
                                        "{0}\{1}.mdf",
                                        sourceSrv.MasterDBPath,
                                        databaseName))
dataFile.GrowthType = FileGrowthType.KB
dataFile.Growth = 10240
dataFile.IsPrimaryFile = True

Dim logFile = New LogFile(
                          db,
                          databaseName,
                          String.Format(
                                        "{0}\{1}_log.ldf",
                                        sourceSrv.MasterDBPath,
                                        databaseName))
logFile.GrowthType = FileGrowthType.KB
logFile.Growth = 10240

db.LogFiles.Add(logFile)

db.Create() '<-- THROWS EXCEPTION ("The PRIMARY filegroup must have at least one file")

看起来 dataFile 必须显式添加到 fileGroup(它不会添加自己,即使它已将 fileGroup 作为其构造函数的第一个参数传递)。

dataFile.Growth = 10240
dataFile.IsPrimaryFile = True

fileGroup.Files.Add(dataFile)

原始代码中还有一个小错误(.ldf 名称不唯一,fileGroup.IsDefault 设置为 True 导致错误)。

这是完整的工作代码:

Dim serverName = "(local)"
Dim databaseName = "TestNew5"

Dim sourceSrv = New Server(serverName)

Dim db As Database
db = New Database(sourceSrv, databaseName)

db.AutoCreateStatisticsEnabled = True
db.AutoUpdateStatisticsEnabled = True
db.AutoUpdateStatisticsAsync = True

Dim fileGroup = New FileGroup(db, "PRIMARY")

Dim dataFile = New DataFile(
                           fileGroup,
                           databaseName,
                           String.Format(
                                        "{0}\{1}.mdf",
                                        sourceSrv.MasterDBPath,
                                        databaseName))
dataFile.GrowthType = FileGrowthType.KB
dataFile.Growth = 10240
dataFile.IsPrimaryFile = True

fileGroup.Files.Add(dataFile)

db.FileGroups.Add(fileGroup)

Dim logFile = New LogFile(
                          db,
                          databaseName + "_log",
                          String.Format(
                                        "{0}\{1}_1.ldf",
                                        sourceSrv.MasterDBPath,
                                        databaseName))
logFile.GrowthType = FileGrowthType.KB
logFile.Growth = 10240

db.LogFiles.Add(logFile)

db.Create()