Пример #1
0
        internal EntityAdapter(Type classType)
        {
            Initialize();
            this.EntityName = classType.Name;
            this.FullName = classType.FullName;

            ORMTableAttribute[] tableAttributes = (ORMTableAttribute[])classType.GetCustomAttributes(typeof(ORMTableAttribute), false);
            if (tableAttributes != null && tableAttributes.Length > 0)
            {
                this.TableName = tableAttributes[0].Name;

                StringBuilder sbAf = new StringBuilder();
                StringBuilder sbId = new StringBuilder();
                StringBuilder sbPk = new StringBuilder();
                StringBuilder sbNames = new StringBuilder();
                FieldAdapter fieldAdapter;
                foreach (PropertyInfo propertyInfo in classType.GetProperties())
                {
                    fieldAdapter = new FieldAdapter();
                    ORMColumnAttribute[] columnAttributes = (ORMColumnAttribute[])propertyInfo.GetCustomAttributes(typeof(ORMColumnAttribute), false);
                    if (columnAttributes.Length == 0)
                    {
                        continue;
                    }
                    ORMColumnAttribute columnAttribute = columnAttributes[0];
                    if (columnAttribute.IsAutoFill)
                    {
                        fieldAdapter.IsAutoFill = true;
                        sbAf.Append(",");
                        sbAf.Append(propertyInfo.Name);
                    }
                    if (columnAttribute.IsIdentity)
                    {
                        fieldAdapter.IsIdentity = true;
                        sbId.Append(",");
                        sbId.Append(propertyInfo.Name);
                    }
                    if (columnAttribute.IsPrimaryKey)
                    {
                        fieldAdapter.IsPrimaryKey = true;
                        sbPk.Append(",");
                        sbPk.Append(propertyInfo.Name);
                    }
                    sbNames.Append(",");
                    sbNames.Append(propertyInfo.Name);

                    fieldAdapter.Name = propertyInfo.Name;
                    fieldAdapter.PropertyTypeName = propertyInfo.PropertyType.FullName;
                    fieldAdapter.PropertyType = propertyInfo.PropertyType;

                    fieldAdapter.ColumnName = columnAttribute.Name;
                    fieldAdapter.DbType = columnAttribute.DbType;
                    fieldAdapter.DbTypeName = columnAttribute.DbType.GetType().FullName;

                    this._keyMap.Add(propertyInfo.Name, columnAttribute.Name);
                    this._keyDbType.Add(propertyInfo.Name, columnAttribute.DbType);

                    this._mapColumnName.Add(fieldAdapter.ColumnName, fieldAdapter);
                    this._mapPropteryName.Add(fieldAdapter.Name, fieldAdapter);
                }
                this._keyMap.Add("$Af", sbAf.ToString()); // 找出所有 AutoFill(数据库自动填入项)
                this._keyMap.Add("$Id", sbId.ToString()); // 找出标记为 Id 的项
                this._keyMap.Add("$Pk", sbPk.ToString()); // 找出标记为 主键 的项
                this._keyMap.Add("$Names", sbNames.ToString()); //所有字段名
            }
        }
Пример #2
0
        private void Intitalize(DataSet dataSet)
        {
            DataTable table = dataSet.Tables["Entity"];
            this.TableName = table.Rows[0]["Table"].ToString();
            this.EntityName = table.Rows[0]["Name"].ToString();
            this.FullName = table.Rows[0]["NameSpace"].ToString() + "." + this.EntityName;

            StringBuilder sbAf = new StringBuilder();
            StringBuilder sbId = new StringBuilder();
            StringBuilder sbPk = new StringBuilder();
            StringBuilder sbNames = new StringBuilder();
            FieldAdapter fieldAdapter;

            table = dataSet.Tables["Proptery"];
            foreach (DataRow row in table.Rows)
            {
                fieldAdapter = new FieldAdapter();

                if (row.Table.Columns.Contains("IsAutoFill"))
                {
                    if ("true".Equals(row["IsAutoFill"].ToString().ToLower()))
                    {
                        fieldAdapter.IsAutoFill = true;
                        sbAf.Append(",");
                        sbAf.Append(row["Name"].ToString());
                    }
                }
                if (row.Table.Columns.Contains("IsIdentity"))
                {
                    if ("true".Equals(row["IsIdentity"].ToString().ToLower()))
                    {
                        fieldAdapter.IsIdentity = true;
                        sbId.Append(",");
                        sbId.Append(row["Name"].ToString());
                    }
                }
                if (row.Table.Columns.Contains("IsPrimaryKey"))
                {
                    if ("true".Equals(row["IsPrimaryKey"].ToString().ToLower()))
                    {
                        fieldAdapter.IsPrimaryKey = true;
                        sbPk.Append(",");
                        sbPk.Append(row["Name"].ToString());
                    }
                }
                sbNames.Append(",");
                sbNames.Append(row["Name"].ToString());

                fieldAdapter.Name = row["Name"].ToString();
                fieldAdapter.PropertyTypeName = row["PropteryType"].ToString();
                fieldAdapter.PropertyType = Type.GetType(fieldAdapter.PropertyTypeName);

                fieldAdapter.ColumnName = row["Column"].ToString();
                fieldAdapter.DbTypeName = row["DbType"].ToString();
                string dbTypeName = fieldAdapter.DbTypeName.Substring(fieldAdapter.DbTypeName.LastIndexOf('.') + 1);
                fieldAdapter.DbType = (DbType)Enum.Parse(typeof(DbType), dbTypeName);

                this._keyMap.Add(row["Name"].ToString(), row["Column"].ToString());
                this._keyDbType.Add(row["Name"].ToString(), fieldAdapter.DbType);

                this._mapColumnName.Add(fieldAdapter.ColumnName, fieldAdapter);
                this._mapPropteryName.Add(fieldAdapter.Name, fieldAdapter);
            }

            this._keyMap.Add("$Af", sbAf.ToString()); // 找出所有 AutoFill(数据库自动填入项)
            this._keyMap.Add("$Id", sbId.ToString()); // 找出标记为 Id 的项
            this._keyMap.Add("$Pk", sbPk.ToString()); // 找出标记为 主键 的项
            this._keyMap.Add("$Names", sbNames.ToString()); //所有字段名

            dataSet.Dispose();
        }