/// <summary>
 /// Deprecated Method for adding a new object to the SchemaChanges EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSchemaChanges(SchemaChange schemaChange)
 {
     base.AddObject("SchemaChanges", schemaChange);
 }
 /// <summary>
 /// Create a new SchemaChange object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="majorVersion">Initial value of the MajorVersion property.</param>
 /// <param name="minorVersion">Initial value of the MinorVersion property.</param>
 /// <param name="scriptName">Initial value of the ScriptName property.</param>
 /// <param name="dateApplied">Initial value of the DateApplied property.</param>
 public static SchemaChange CreateSchemaChange(global::System.Int32 id, global::System.Int32 majorVersion, global::System.Int32 minorVersion, global::System.String scriptName, global::System.DateTime dateApplied)
 {
     SchemaChange schemaChange = new SchemaChange();
     schemaChange.id = id;
     schemaChange.MajorVersion = majorVersion;
     schemaChange.MinorVersion = minorVersion;
     schemaChange.ScriptName = scriptName;
     schemaChange.DateApplied = dateApplied;
     return schemaChange;
 }
 /// <summary>
 /// Create a new SchemaChange object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="majorReleaseNumber">Initial value of the MajorReleaseNumber property.</param>
 /// <param name="minorReleaseNumber">Initial value of the MinorReleaseNumber property.</param>
 /// <param name="pointReleaseNumber">Initial value of the PointReleaseNumber property.</param>
 /// <param name="scriptName">Initial value of the ScriptName property.</param>
 /// <param name="dateApplied">Initial value of the DateApplied property.</param>
 /// <param name="description">Initial value of the Description property.</param>
 /// <param name="appliedBy">Initial value of the AppliedBy property.</param>
 public static SchemaChange CreateSchemaChange(global::System.Int32 id, global::System.Int32 majorReleaseNumber, global::System.Int32 minorReleaseNumber, global::System.Int32 pointReleaseNumber, global::System.String scriptName, global::System.DateTime dateApplied, global::System.String description, global::System.String appliedBy)
 {
     SchemaChange schemaChange = new SchemaChange();
     schemaChange.id = id;
     schemaChange.MajorReleaseNumber = majorReleaseNumber;
     schemaChange.MinorReleaseNumber = minorReleaseNumber;
     schemaChange.PointReleaseNumber = pointReleaseNumber;
     schemaChange.ScriptName = scriptName;
     schemaChange.DateApplied = dateApplied;
     schemaChange.Description = description;
     schemaChange.AppliedBy = appliedBy;
     return schemaChange;
 }
        static void Main(string[] args)
        {
            string connectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True", args[0], args[1]);
            string efConnectionString = string.Format("metadata=res://*/Database.csdl|res://*/Database.ssdl|res://*/Database.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"", connectionString);
            DatabaseEntities db = new DatabaseEntities(efConnectionString);

            SchemaChange latestChange = (from changes in db.SchemaChanges
                                      orderby changes.MajorVersion descending, changes.MinorVersion descending
                                      select changes).FirstOrDefault<SchemaChange>();

            if (latestChange == null)
            {
                throw new ApplicationException("The SchemaChange table in your destination database is empty.  It must contain at least 1 baseline record.");
            }

            string filePath = args[2];
            if (filePath.Substring(filePath.Length-1) != @"\")
            {
                filePath += @"\";
            }

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.Text;

                    DirectoryInfo schemaDir = new DirectoryInfo(filePath + "schema");

                    foreach (FileInfo script in schemaDir.GetFiles("*.sql")
                                .Where(x => Int32.Parse(x.Name.Split('.')[0]) == latestChange.MajorVersion
                                && Int32.Parse(x.Name.Split('.')[1]) > latestChange.MinorVersion))
                    {
                        string command = script.OpenText().ReadToEnd();
                        command = Regex.Replace(command, @"(?I)\bGO\b", "\0");

                        foreach (string commandFragment in command.Split('\0'))
                        {
                            cmd.CommandText = commandFragment;
                            cmd.ExecuteNonQuery();
                        }

                        SchemaChange newChange = new SchemaChange();
                        newChange.MajorVersion = int.Parse(script.Name.Split('.')[0]);
                        newChange.MinorVersion = int.Parse(script.Name.Split('.')[1]);
                        newChange.ScriptName = script.Name;
                        newChange.DateApplied = DateTime.Now;
                        db.SchemaChanges.AddObject(newChange);
                        db.SaveChanges();
                    }

                    RemoveFuncs(cmd);
                    AddFuncs(cmd, filePath);
                    RemoveProcs(cmd);
                    AddProcs(cmd, filePath);
                    RemoveViews(cmd);
                    AddViews(cmd, filePath);
                }
            }
        }