title: C#: SMO problems with dependencies / Multipe Create Table statements
body: <p>I'm trying to generate a SQL script from my existing database by using SMO.
  This script will be used later to restore the database without data. 
  For this, I set up a method that looks like this: </p>
  
  <pre><code>public void GenerateSQLScripts(string dbName)
      {
  
          StringBuilder sb = new StringBuilder();
          Server server = new Server(SqlServer);
          Database db = server.Databases[dbName];
  
  
          var scriptopt = new ScriptingOptions();
          scriptopt.TargetServerVersion = SqlServerVersion.Version105; // Windows 2008 R2
          scriptopt.AnsiPadding = true;
          scriptopt.WithDependencies = true;
          scriptopt.IncludeHeaders = true;
          scriptopt.SchemaQualify = true;
          scriptopt.ExtendedProperties = true;
          scriptopt.TargetDatabaseEngineType = DatabaseEngineType.Standalone;
          scriptopt.IncludeDatabaseContext = true;
          scriptopt.ScriptDrops = false;
          scriptopt.ScriptData = false;
          scriptopt.ScriptSchema = true;
          scriptopt.DriAllConstraints = true;
          scriptopt.DriForeignKeys = true;
          scriptopt.Indexes = true;
          scriptopt.DriPrimaryKey = true;
          scriptopt.DriUniqueKeys = true;
          scriptopt.DriChecks = true;
          scriptopt.AllowSystemObjects = false;
          scriptopt.AppendToFile = false;
          scriptopt.ScriptBatchTerminator = true;
  
  
          // script Tables
          foreach (Table t in db.Tables)
          {
              if (!t.IsSystemObject)
              {
                  StringCollection sc = t.Script(scriptopt);
                  foreach (string s in sc)
                  {
                      sb.AppendLine(s);
                  }
              }
  
          }
  
          //Script Stored Procedures
          foreach (StoredProcedure sp in db.StoredProcedures)
          {
              if (!sp.IsSystemObject)
              {
                  var sc = sp.Script(scriptopt);
                  foreach (string s in sc)
                  {
                      sb.AppendLine(s);
                  }
              } 
  
          }
  
          //Views
          foreach(View v in db.Views){
              if (!v.IsSystemObject)
              {
                  StringCollection sc = v.Script(scriptopt);
                  foreach (string s in sc)
                  {
                      sb.AppendLine(s);
                  }
              }
  
          }
          File.WriteAllText(Path, sb.ToString());
  
      } 
  </code></pre>
  
  <p>This method creates a SQL file that contains multiple <code>Create Table</code> statements for the same tables. This happens every time a table is referenced, regardless if it exists or not. 
  I found a solution for this problem by turning WithDependencies off:</p>
  
  <pre><code>scriptopt.WithDependencies = false;
  </code></pre>
  
  <p>Now the script is much smaller since there are no additional statements. But since there is no order in my statements, some tables are referencing to other tables that do not exist at that time.
  After some research I found the class <a href="https://docs.microsoft.com/de-de/dotnet/api/microsoft.sqlserver.management.smo.dependencywalker?view=sqlserver-2016" rel="nofollow noreferrer">DependencyWalker</a>.</p>
  
  <p>I changed my code to use this class and to sort the order of statements in order to restore my database:</p>
  
  <pre><code>        UrnCollection ucol = new UrnCollection();
          foreach(Table t in db.Tables){
              ucol.Add(t.Urn);
          }
  
          DependencyWalker dw = new DependencyWalker(server);
  
          DependencyTree dtree = dw.DiscoverDependencies(ucol.ToArray(), DependencyType.Parents);
  
          DependencyCollection dcol = dw.WalkDependencies(dtree);
  
  // ScriptingOptions
  
          Scripter scripter = new Scripter(server);
          foreach (Table t in db.Tables)
          {
  
              if (!t.IsSystemObject)
              {
                  scripter.Options = scriptopt;
                  scripter.Options.WithDependencies = false;
                  IEnumerable&lt;string&gt; tscripts = scripter.EnumScriptWithList(dcol);
                  foreach (string s in tscripts)
                  {
                      sb.AppendLine(s);
                  }
              }
          }
  </code></pre>
  
  <p>The rest of the source code remains unaffected due to testing purposes.
  Now I got the same error as before. My script is filled with multiple <code>Create Table</code> statements which leads to <code>Table already exists</code> errors even though the option is turned off .</p>