private DatabaseEntityPropertyDef CreatePropertyDef(DatabaseEntityDef modelDef, PropertyInfo info) { DatabaseEntityPropertyDef propertyDef = new DatabaseEntityPropertyDef(); #region 自身 propertyDef.EntityDef = modelDef; propertyDef.PropertyName = info.Name; propertyDef.PropertyType = info.PropertyType; propertyDef.GetMethod = info.GetGetMethod(); propertyDef.SetMethod = info.GetSetMethod(); #endregion #region 数据库 IEnumerable <Attribute> propertyAttrs = info.GetCustomAttributes(typeof(EntityPropertyAttribute), false).Select(o => (Attribute)o); if (propertyAttrs != null && propertyAttrs.Count() > 0) { EntityPropertyAttribute propertyAttr = propertyAttrs.ElementAt(0) as EntityPropertyAttribute; propertyDef.IsTableProperty = true; propertyDef.IsNullable = !propertyAttr.NotNull; propertyDef.IsUnique = propertyAttr.Unique; propertyDef.DbLength = propertyAttr.Length > 0 ? (int?)propertyAttr.Length : null; propertyDef.IsLengthFixed = propertyAttr.FixedLength; propertyDef.DbDefaultValue = ValueConverter.TypeValueToDbValue(propertyAttr.DefaultValue); propertyDef.DbDescription = propertyAttr.Description; if (propertyAttr.ConverterType != null) { propertyDef.TypeConverter = _typeConverterFactory.GetTypeConverter(propertyAttr.ConverterType); } } //判断是否是主键 IEnumerable <Attribute> atts1 = info.GetCustomAttributes(typeof(AutoIncrementPrimaryKeyAttribute), false).Select <object, Attribute>(o => (Attribute)o); if (atts1 != null && atts1.Count() > 0) { propertyDef.IsTableProperty = true; propertyDef.IsAutoIncrementPrimaryKey = true; propertyDef.IsNullable = false; propertyDef.IsForeignKey = false; propertyDef.IsUnique = true; } else { //判断是否外键 IEnumerable <Attribute> atts2 = info.GetCustomAttributes(typeof(ForeignKeyAttribute), false).Select <object, Attribute>(o => (Attribute)o); if (atts2 != null && atts2.Count() > 0) { propertyDef.IsTableProperty = true; propertyDef.IsAutoIncrementPrimaryKey = false; propertyDef.IsForeignKey = true; //propertyDef.IsNullable = false; propertyDef.IsUnique = false; } } if (propertyDef.IsTableProperty) { propertyDef.DbReservedName = _databaseEngine.GetReservedStatement(propertyDef.PropertyName); propertyDef.DbParameterizedName = _databaseEngine.GetParameterizedStatement(propertyDef.PropertyName); if (propertyDef.TypeConverter != null) { propertyDef.DbFieldType = propertyDef.TypeConverter.TypeToDbType(propertyDef.PropertyType); } else { propertyDef.DbFieldType = _databaseEngine.GetDbType(propertyDef.PropertyType); } } #endregion return(propertyDef); }