예제 #1
0
        public static void EntityIsMappingDatabase(Type type, string message)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(type);

            if (entityInfo.MappingTableAttribute == null)
            {
                throw new ORMException(message);
            }
            if (entityInfo.GetKeyFieldInfos() == null || entityInfo.GetKeyFieldInfos().Count == 0)
            {
                throw new ORMException(message);
            }
        }
예제 #2
0
        private string GetUpdateStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList)
        {
            fieldPropertyList = new List <PropertyInfo>();
            string sets  = "";
            string query = "";

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (query != "")
                {
                    query += " AND ";
                }
                query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            foreach (SchemaItem mfi in entityInfo.GetNeedUpdateFieldInfos())
            {
                if (sets != "")
                {
                    sets += ",";
                }
                sets += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            return(string.Format("{0} WHERE {1}", sets, query));
        }
예제 #3
0
        public static void CheckEntityKey(object entity)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entity.GetType());

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (mfi.ProInfo.GetValue(entity, null) == null)//如果对象的属性为null,则把此参数设置为DBNull
                {
                    throw new ORMException(ErrorMessages.PrimaryKeyIsNull);
                }
            }
        }
예제 #4
0
        public static string GetEntityInfoMessage(object entity)
        {
            TypeSchema    entityInfo  = ORMSchemaCache.GetTypeSchema(entity.GetType());
            StringBuilder infoBuilder = new StringBuilder();

            infoBuilder.AppendLine("Entity Type: " + entity.GetType().FullName);
            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                PropertyInfo property = mfi.ProInfo;
                infoBuilder.AppendLine("[" + property.Name + "]: " + property.GetValue(entity, null));
            }
            return(infoBuilder.ToString());
        }
예제 #5
0
        private string GetDeleteStatement(TypeSchema entityInfo, out List <PropertyInfo> fieldPropertyList)
        {
            string query = "";

            fieldPropertyList = new List <PropertyInfo>();
            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                if (query != "")
                {
                    query += " AND ";
                }
                query += GetQuotedName(mfi.MappingFieldAttribute.FieldName) + "=@" + mfi.ProInfo.Name;
                fieldPropertyList.Add(mfi.ProInfo);
            }
            return(string.Format("WHERE {0}", query));
        }
예제 #6
0
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType      dbType    = ORMHelper.GetDbTypeByName(type.Name);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType        = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return(dbCommand);
        }