/// <summary>
        /// Sets up the databases
        /// </summary>
        public virtual void Setup()
        {
            foreach (IDatabase Key in Mappings.Keys)
            {
                if (Key.Update)
                {
                    string DatabaseName = Regex.Match(Key.ConnectionString, "Initial Catalog=(.*?;)").Value.Replace("Initial Catalog=", "").Replace(";", "");
                    Utilities.SQL.DataClasses.Database TempDatabase = new SQL.DataClasses.Database(DatabaseName);
                    SetupFunctions(TempDatabase);
                    SetupTables(Key, TempDatabase);
                    SetupJoiningTables(Key, TempDatabase);
                    SetupAuditTables(Key, TempDatabase);

                    DatabaseStructures.Add(TempDatabase);
                    foreach (Utilities.SQL.DataClasses.Table Table in TempDatabase.Tables)
                    {
                        Table.SetupForeignKeys();
                    }
                    Utilities.SQL.DataClasses.Database CurrentDatabase = SQL.SQLServer.SQLServer.GetDatabaseStructure(Key.ConnectionString);
                    SQL.SQLServer.SQLServer.UpdateDatabase(TempDatabase, CurrentDatabase, Key.ConnectionString);

                    foreach (IMapping Mapping in Mappings[Key])
                    {
                        foreach (IProperty Property in Mapping.Properties)
                        {
                            Property.SetupLoadCommands();
                        }
                    }
                }
            }
        }
 private void SetupJoiningTables(IDatabase Key, SQL.DataClasses.Database TempDatabase)
 {
     foreach (IMapping Mapping in Mappings[Key])
     {
         foreach (IProperty Property in Mapping.Properties)
         {
             if (Property is IMap)
             {
                 IMapping MapMapping = Mappings[Key].First(x => x.ObjectType == Property.Type);
                 TempDatabase[Mapping.TableName].AddColumn(Property.FieldName,
                                                           MapMapping.IDProperty.Type.ToDbType(),
                                                           MapMapping.IDProperty.MaxLength,
                                                           !Property.NotNull,
                                                           false,
                                                           Property.Index,
                                                           false,
                                                           false,
                                                           MapMapping.TableName,
                                                           MapMapping.IDProperty.FieldName,
                                                           "",
                                                           false,
                                                           false,
                                                           Mapping.Properties.Count(x => x.Type == Property.Type) == 1 && Mapping.ObjectType != Property.Type);
             }
             else if (Property is IManyToOne || Property is IManyToMany || Property is IIEnumerableManyToOne || Property is IListManyToMany || Property is IListManyToOne)
             {
                 SetupJoiningTablesEnumerable(Mapping, Property, Key, TempDatabase);
             }
         }
     }
 }
 private void SetupTables(IDatabase Key, SQL.DataClasses.Database TempDatabase)
 {
     foreach (IMapping Mapping in Mappings[Key])
     {
         TempDatabase.AddTable(Mapping.TableName);
         SetupProperties(TempDatabase[Mapping.TableName], Mapping);
     }
 }
 private void SetupAuditTables(IDatabase Key, SQL.DataClasses.Database TempDatabase)
 {
     System.Collections.Generic.List <Utilities.SQL.DataClasses.Table> TempTables = new System.Collections.Generic.List <Utilities.SQL.DataClasses.Table>();
     foreach (Utilities.SQL.DataClasses.Table Table in TempDatabase.Tables)
     {
         TempTables.Add(SetupAuditTables(Table));
         SetupInsertUpdateTrigger(Table);
         SetupDeleteTrigger(Table);
     }
     TempDatabase.Tables.AddRange(TempTables);
 }
        /// <summary>
        /// Sets up the databases
        /// </summary>
        public virtual void Setup()
        {
            foreach (IDatabase Key in Mappings.Keys)
            {
                if (Key.Update)
                {
                    string DatabaseName = Regex.Match(Key.ConnectionString, "Initial Catalog=(.*?;)").Value.Replace("Initial Catalog=", "").Replace(";", "");
                    Utilities.SQL.DataClasses.Database TempDatabase = new SQL.DataClasses.Database(DatabaseName);
                    SetupFunctions(TempDatabase);
                    SetupTables(Key, TempDatabase);
                    SetupJoiningTables(Key, TempDatabase);
                    SetupAuditTables(Key, TempDatabase);

                    DatabaseStructures.Add(TempDatabase);
                    foreach (Utilities.SQL.DataClasses.Table Table in TempDatabase.Tables)
                    {
                        Table.SetupForeignKeys();
                    }
                    Utilities.SQL.DataClasses.Database CurrentDatabase = SQL.SQLServer.SQLServer.GetDatabaseStructure(Key.ConnectionString);
                    SQL.SQLServer.SQLServer.UpdateDatabase(TempDatabase, CurrentDatabase, Key.ConnectionString);

                    foreach (IMapping Mapping in Mappings[Key])
                    {
                        foreach (IProperty Property in Mapping.Properties)
                        {
                            Property.SetupLoadCommands();
                        }
                    }
                }
            }
        }
 private void SetupFunctions(SQL.DataClasses.Database Database)
 {
     Database.AddFunction("Split", "CREATE FUNCTION dbo.Split(@List nvarchar(MAX),@SplitOn nvarchar(5)) RETURNS @ReturnValue table(Id int identity(1,1),Value nvarchar(200)) AS  BEGIN While (Charindex(@SplitOn,@List)>0) Begin Insert Into @ReturnValue (value) Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List)) End Insert Into @ReturnValue (Value) Select Value = ltrim(rtrim(@List)) Return END");
 }
        private void SetupJoiningTablesEnumerable(IMapping Mapping, IProperty Property, IDatabase Key, SQL.DataClasses.Database TempDatabase)
        {
            if (TempDatabase.Tables.FirstOrDefault(x => x.Name == Property.TableName) != null)
            {
                return;
            }
            IMapping MapMapping = Mappings[Key].First(x => x.ObjectType == Property.Type);

            if (MapMapping == Mapping)
            {
                TempDatabase.AddTable(Property.TableName);
                TempDatabase[Property.TableName].AddColumn("ID_", DbType.Int32, 0, false, true, true, true, false, "", "", "");
                TempDatabase[Property.TableName].AddColumn(Mapping.TableName + Mapping.IDProperty.FieldName,
                                                           Mapping.IDProperty.Type.ToDbType(),
                                                           Mapping.IDProperty.MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           Mapping.TableName,
                                                           Mapping.IDProperty.FieldName,
                                                           "",
                                                           false,
                                                           false,
                                                           false);
                TempDatabase[Property.TableName].AddColumn(MapMapping.TableName + MapMapping.IDProperty.FieldName + "2",
                                                           MapMapping.IDProperty.Type.ToDbType(),
                                                           MapMapping.IDProperty.MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           MapMapping.TableName,
                                                           MapMapping.IDProperty.FieldName,
                                                           "",
                                                           false,
                                                           false,
                                                           false);
            }
            else
            {
                TempDatabase.AddTable(Property.TableName);
                TempDatabase[Property.TableName].AddColumn("ID_", DbType.Int32, 0, false, true, true, true, false, "", "", "");
                TempDatabase[Property.TableName].AddColumn(Mapping.TableName + Mapping.IDProperty.FieldName,
                                                           Mapping.IDProperty.Type.ToDbType(),
                                                           Mapping.IDProperty.MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           Mapping.TableName,
                                                           Mapping.IDProperty.FieldName,
                                                           "",
                                                           true,
                                                           false,
                                                           false);
                TempDatabase[Property.TableName].AddColumn(MapMapping.TableName + MapMapping.IDProperty.FieldName,
                                                           MapMapping.IDProperty.Type.ToDbType(),
                                                           MapMapping.IDProperty.MaxLength,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           false,
                                                           MapMapping.TableName,
                                                           MapMapping.IDProperty.FieldName,
                                                           "",
                                                           true,
                                                           false,
                                                           false);
            }
        }