Example #1
0
        /// <summary>
        /// 初始化状态[继承此基类的实体在构造函数中需调用此方法]
        /// </summary>
        /// <param name="entityInstance">实体对象,一般写:this</param>
        /// <param name="tableName">表名,如:Users</param>
        /// <param name="conn">数据链接,单数据库时可写Null,或写默认链接配置项:"Conn",或直接数据库链接字符串</param>
        protected void SetInit(Object entityInstance, string tableName, string conn)
        {
            conn     = string.IsNullOrEmpty(conn) ? AppConfig.DB.DefaultConn : conn;
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + MD5.Get(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = TableSchema.GetColumns(typeInfo);
                        if (!DBTool.ExistsTable(tableName, conn))
                        {
                            if (!DBTool.CreateTable(tableName, Columns, conn))
                            {
                                Error.Throw("Create Table Error:" + tableName);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = TableSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Add(key, Columns, null, 1440);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    action.SetAopOff();
                }
                action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.WriteLogToTxt(err);
                }
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// 将原有的初始化改造成延时加载。
        /// </summary>
        private void SetDelayInit(Object entityInstance, string tableName, string conn, AopOp op)
        {
            if (string.IsNullOrEmpty(conn))
            {
                //不设置链接,则忽略(当成普通的实体类)
                return;
            }
            entity   = entityInstance;
            typeInfo = entity.GetType();
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = typeInfo.Name;
                    if (tableName.EndsWith(AppConfig.EntitySuffix))
                    {
                        tableName = tableName.Substring(0, tableName.Length - AppConfig.EntitySuffix.Length);
                    }
                }

                string key = tableName + StaticTool.GetHashKey(conn);
                if (!CacheManage.LocalInstance.Contains(key))
                {
                    DalType dal      = DBTool.GetDalType(conn);
                    bool    isTxtDal = dal == DalType.Txt || dal == DalType.Xml;
                    string  errMsg   = string.Empty;
                    Columns = DBTool.GetColumns(tableName, conn, out errMsg);//内部链接错误时抛异常。
                    if (Columns == null || Columns.Count == 0)
                    {
                        if (errMsg != string.Empty)
                        {
                            Error.Throw(errMsg);
                        }
                        Columns = ColumnSchema.GetColumns(typeInfo);
                        ConnBean connBean = ConnBean.Create(conn);//下面指定链接,才不会在主从备时被切换到其它库。
                        if (!DBTool.ExistsTable(tableName, connBean.ConnString))
                        {
                            DBTool.ErrorMsg = null;
                            if (!DBTool.CreateTable(tableName, Columns, connBean.ConnString))
                            {
                                Error.Throw("SimpleOrmBase :Create Table " + tableName + " Error:" + DBTool.ErrorMsg);
                            }
                        }
                    }
                    else if (isTxtDal)//文本数据库
                    {
                        if (FieldSource != FieldSource.Data)
                        {
                            MDataColumn c2 = ColumnSchema.GetColumns(typeInfo);
                            if (FieldSource == FieldSource.BothOfAll)
                            {
                                Columns.AddRange(c2);
                            }
                            else
                            {
                                Columns = c2;
                            }
                        }
                    }

                    if (Columns != null && Columns.Count > 0)
                    {
                        CacheManage.LocalInstance.Set(key, Columns, 1440, null);
                    }
                }
                else
                {
                    Columns = CacheManage.LocalInstance.Get(key) as MDataColumn;
                }

                _Action = new MAction(Columns.ToRow(tableName), conn);
                if (typeInfo.Name == "SysLogs")
                {
                    _Action.SetAopState(Aop.AopOp.CloseAll);
                }
                else
                {
                    _Action.SetAopState(op);
                }
                _Action.EndTransation();
            }
            catch (Exception err)
            {
                if (typeInfo.Name != "SysLogs")
                {
                    Log.Write(err, LogType.DataBase);
                }
                throw;
            }
        }