Exemplo n.º 1
0
        public ColumnMap(PropertyInfo propertyInfo)
        {
            this.Name            = propertyInfo.Name;
            this.IsReadOnly      = false;
            this.PropertyInfo    = PropertyInfo;
            this.KeyType         = KeyType.NotAKey;
            this.ColumnLength    = 64;
            this.IsRequired      = false;
            this.IsAutoIncrement = false;
            this.PropertyInfo    = propertyInfo;
            this.IdentityType    = IdentityType.Int64Unsigned;

            ColumnAttribute       ca     = propertyInfo.GetCustomAttribute <ColumnAttribute>();
            ColumnIgnoreAttribute ignore = propertyInfo.GetCustomAttribute <ColumnIgnoreAttribute>();

            if (ca != null)
            {
                this.ColumnName   = ca.Name;
                this.KeyType      = ca.KeyType;
                this.IdentityType = ca.IdentityType;
            }
            else
            {
                this.ColumnName = this.Name;
            }

            this.Ignored = ignore != null;
        }
Exemplo n.º 2
0
        public static void Add <TEntity>(DbContext dbContext, TEntity entity, out Dictionary <string, object> paramsDic) where TEntity : class
        {
            dbContext.TableName = TableAttribute.GetName(typeof(TEntity));
            paramsDic           = new Dictionary <string, object>();

            StringBuilder builder_front = new StringBuilder(), builder_behind = new StringBuilder();

            builder_front.Append("INSERT INTO ");
            builder_front.Append(dbContext.TableName);
            builder_front.Append(" (");
            builder_behind.Append(" VALUES (");

            PropertyInfo[] propertyInfos = GetPropertiesDicByType(typeof(TEntity));
            string         columnName    = string.Empty;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                //if not column mark exist,jump to next
                if (ColumnIgnoreAttribute.Exist(typeof(TEntity)))
                {
                }
                //AutoIncrease : if property is auto increase attribute skip this column.
                else if (propertyInfo.GetCustomAttribute(typeof(AutoIncreaseAttribute), true) is AutoIncreaseAttribute autoIncreaseAttr)
                {
                }
                //Column
                else if (propertyInfo.GetCustomAttribute(typeof(ColumnAttribute), true) is ColumnAttribute column)
                {
                    builder_front.Append(column.GetName(propertyInfo.Name));
                    builder_front.Append(",");
                    builder_behind.Append("@");
                    //multitype database support
                    switch (dbContext.DataBaseType)
                    {
                    case DataBaseType.SqlServer:
                        columnName = column.GetName(propertyInfo.Name).Replace("[", "").Replace("]", "");
                        break;

                    case DataBaseType.MySql:
                        columnName = column.GetName(propertyInfo.Name).Replace("`", "");
                        break;

                    default:
                        //default 兼容mysql和sqlserver的系统字段
                        columnName = column.GetName(propertyInfo.Name).Replace("[", "").Replace("]", "").Replace("`", "");
                        break;
                    }
                    builder_behind.Append(columnName);
                    builder_behind.Append(",");
                    if (!paramsDic.ContainsKey(columnName))
                    {
                        paramsDic.Add(columnName, propertyInfo.GetValue(entity));
                    }
                }

                //in the end,remove the redundant symbol of ','
                if (propertyInfos.Last() == propertyInfo)
                {
                    builder_front.Remove(builder_front.Length - 1, 1);
                    builder_front.Append(")");

                    builder_behind.Remove(builder_behind.Length - 1, 1);
                    builder_behind.Append(")");
                }
            }
            //Generate SqlStatement
            dbContext.SqlStatement = builder_front.Append(builder_behind.ToString()).ToString();
        }