예제 #1
0
        /// <summary>
        /// 创建、更新数据库结构,添加 更新、删除索引, 列只增加,不修改,不删除
        /// </summary>
        /// <typeparam name="T">EntityContext</typeparam>
        public virtual void Build <T>() where T : EntityContext
        {
            bool count = databaseSchema.Exist();

            if (count == false)
            {
                databaseSchema.CreateDatabase();
            }

            List <TableInfoModel> tables     = tableSchema.GetTables();
            List <Type>           modelTypes = tableSchema.GetModelType <T>();

            foreach (var t in modelTypes)
            {
                var tb = tableSchema.GetTableName(t);
                if (!tables.Exists(q => string.Equals(q.Name, tb.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    var columns = tableSchema.GetModelColumns(t, tb.Name);
                    tableSchema.CreateTable(tb, columns);
                }
                else
                {
                    var tableColumns            = tableSchema.GetTableColumns(tb.Name);
                    var modelColumns            = tableSchema.GetModelColumns(t, tb.Name);
                    List <IndexModel> addIndexs = new List <IndexModel>();
                    List <string>     delIndexs = new List <string>();
                    foreach (var mColumn in modelColumns)
                    {
                        var tColumn = tableColumns.Find(q => q.Name.Equals(mColumn.Name, StringComparison.OrdinalIgnoreCase));
                        if (tColumn == null)
                        {
                            tableSchema.AddColumn(tb.Name, mColumn);
                            if (mColumn.Indexs != null && mColumn.Indexs.Count > 0)
                            {
                                foreach (var index in mColumn.Indexs)
                                {
                                    if (string.IsNullOrEmpty(index.Name))
                                    {
                                        continue;
                                    }
                                    if (string.IsNullOrEmpty(index.ColumnName))
                                    {
                                        index.ColumnName = mColumn.Name;
                                    }
                                    addIndexs.Add(index);
                                }
                            }
                        }
                        else if (mColumn.Indexs != null && mColumn.Indexs.Count > 0)
                        {
                            // 添加数据库不存在索引
                            foreach (var index in mColumn.Indexs)
                            {
                                if (string.IsNullOrEmpty(index.Name))
                                {
                                    continue;
                                }
                                if (string.IsNullOrEmpty(index.ColumnName))
                                {
                                    index.ColumnName = mColumn.Name;
                                }

                                if (tColumn.Indexs == null || tColumn.Indexs.Count == 0)
                                {
                                    addIndexs.Add(index);
                                }
                                else
                                {
                                    var tindex = tColumn.Indexs.Find(q => index.Name.Equals(q.Name, StringComparison.OrdinalIgnoreCase));
                                    if (tindex == null || tindex.IsUnique != index.IsUnique)
                                    {
                                        addIndexs.Add(index);
                                    }
                                }
                            }

                            // 删除数据库多余索引
                            if (tColumn.Indexs != null && tColumn.Indexs.Count > 0)
                            {
                                foreach (var tindex in tColumn.Indexs)
                                {
                                    if (mColumn.Indexs == null || mColumn.Indexs.Count == 0)
                                    {
                                        if (!delIndexs.Contains(tindex.Name, StringComparer.OrdinalIgnoreCase))
                                        {
                                            delIndexs.Add(tindex.Name);
                                        }
                                    }
                                    else
                                    {
                                        var index = mColumn.Indexs.Find(q => tindex.Name.Equals(q.Name, StringComparison.OrdinalIgnoreCase));
                                        if (index == null || tindex.IsUnique != index.IsUnique)
                                        {
                                            if (!delIndexs.Contains(tindex.Name, StringComparer.OrdinalIgnoreCase))
                                            {
                                                delIndexs.Add(tindex.Name);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    foreach (var indexs in delIndexs)
                    {
                        tableSchema.DeleteIndex(tb.Name, indexs);
                    }

                    if (addIndexs.Count > 0)
                    {
                        tableSchema.AddIndex(tb.Name, addIndexs);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 创建、更新数据库结构,添加 更新、删除索引, 列只增加,不修改,不删除
        /// </summary>
        /// <typeparam name="T">EntityContext</typeparam>
        public virtual void Build <T>() where T : EntityContext
        {
            Type dbContextType = typeof(T);
            bool count         = databaseSchema.Exist();

            if (count == false)
            {
                databaseSchema.CreateDatabase();
            }

            List <string> tables     = tableSchema.GetTables();
            List <Type>   modelTypes = tableSchema.GetModelType(dbContextType);

            foreach (var t in modelTypes)
            {
                string tb = tableSchema.GetTableName(t);
                if (!tables.Contains(tb, StringComparer.OrdinalIgnoreCase))
                {
                    var columns = tableSchema.GetModelColumns(t, tb);
                    tableSchema.CreateTable(tb, columns);
                }
                else
                {
                    var tableColumns = tableSchema.GetTableColumns(tb);
                    var modelColumns = tableSchema.GetModelColumns(t, tb);
                    List <ColumnInfoModel> addIndexs = new List <ColumnInfoModel>();
                    List <string>          delIndexs = new List <string>();
                    foreach (var mColumn in modelColumns)
                    {
                        var tColumn = tableColumns.Find(q => string.Compare(q.Name, mColumn.Name, true) == 0);
                        if (tColumn == null)
                        {
                            tableSchema.AddColumn(tb, mColumn);
                        }

                        if (!mColumn.IsKey)
                        {
                            if (tColumn != null && tColumn.IndexName == null)
                            {
                                tColumn.IndexName = string.Empty;
                            }
                            if (mColumn.IndexName == null)
                            {
                                mColumn.IndexName = string.Empty;
                            }

                            if (!string.IsNullOrEmpty(mColumn.IndexName) &&
                                (tColumn == null || tColumn != null &&
                                 (!string.Equals(mColumn.IndexName, tColumn.IndexName,
                                                 StringComparison.OrdinalIgnoreCase) ||
                                  mColumn.IsUnique != tColumn.IsUnique)))
                            {
                                addIndexs.Add(mColumn);
                            }

                            if (tColumn != null && !string.IsNullOrEmpty(tColumn.IndexName) &&
                                (!string.Equals(tColumn.IndexName, mColumn.IndexName, StringComparison.OrdinalIgnoreCase) ||
                                 mColumn.IsUnique != tColumn.IsUnique))
                            {
                                if (!delIndexs.Contains(tColumn.IndexName, StringComparer.OrdinalIgnoreCase))
                                {
                                    delIndexs.Add(tColumn.IndexName);
                                }
                            }
                        }
                    }
                    foreach (var indexs in delIndexs)
                    {
                        tableSchema.DeleteIndex(tb, indexs);
                    }
                    tableSchema.AddIndex(tb, addIndexs);
                }
            }
        }