C # SMO - no scripting constraints

I am using SMO in C # to script the entire schema from a database.

Here is the code I'm using:

  // Instanciando
  Server srv = new Server(con);

  // Reference the database.  
  Database db = srv.Databases[ConfigurationManager.AppSettings["DB"].ToString()];

  // Instanciando el scripteador
  Scripter scrp = new Scripter(srv);
  var urns = new List<Urn>();

  // Propiedades del script
  scrp.Options.IncludeHeaders = true;
  scrp.Options.SchemaQualify = true;
  scrp.Options.SchemaQualifyForeignKeysReferences = true;
  scrp.Options.NoCollation = true;
  scrp.Options.DriAllConstraints = true;
  scrp.Options.DriAll = true;
  scrp.Options.DriAllKeys = true;
  scrp.Options.DriIndexes = true;
  scrp.Options.ClusteredIndexes = true;
  scrp.Options.NonClusteredIndexes = true;
  scrp.Options.ToFileOnly = true;

  // Obteniendo las tablas de la BD   
  foreach (Table tb in db.Tables)
  {
    // check if the table is not a system table
    if(!tb.IsSystemObject)
    {
      urns.Add(tb.Urn);
    }
  }

  // Instanciando un string builder para construir el script
  StringBuilder builder = new StringBuilder();
  System.Collections.Specialized.StringCollection sc = scrp.Script(urns.ToArray());
  foreach (string st in sc)
  {
    // Agregando los comandos al string builder
    builder.AppendLine(st);
    builder.AppendLine("GO"); // Se coloca GO al final de cada statement
  }

  // Escribiendo el archivo
  File.WriteAllText(path, builder.ToString());

      

The problem is that the script is generated without any kind of restriction. (FK, PK, UQ, CK).

What am I doing wrong?

+3


source to share


2 answers


Try using:



scrp.Options.EnforceScriptingOptions = true;

      

+1


source


With these script parameters, I have my limitations:

        Scripter CreateScrp = new Scripter(srv);
        CreateScrp.Options.ScriptDrops = false; // Script drop statements
        CreateScrp.Options.WithDependencies = true; // Walk dependencies
        CreateScrp.Options.DriAllConstraints = true;   // to include referential constraints in the script
        CreateScrp.Options.Triggers = true; // Script triggers
        CreateScrp.Options.NoCollation = false; // Use default collation       
        CreateScrp.Options.ExtendedProperties = true; // Script Extended Properties
        CreateScrp.Options.SchemaQualify = true; // Qualify objects with schema names
        CreateScrp.Options.ScriptSchema = true; // Script schema
        CreateScrp.Options.IncludeDatabaseContext = true;
        CreateScrp.Options.EnforceScriptingOptions = true;
        CreateScrp.ScriptingProgress += CreateScrp_ScriptingProgress;

      



Not sure which one you are missing though ^^

0


source







All Articles