void ConfigureColumnMapping() { var propertyInfos = this.EntityType.PrimitiveProperties.Select(a => a.Property).ToList(); foreach (PropertyInfo propertyInfo in propertyInfos) { IPrimitivePropertyBuilder propertyBuilder = this.Property(propertyInfo.Name); propertyBuilder.IsPrimaryKey(false); propertyBuilder.IsAutoIncrement(false); var propertyAttributes = propertyInfo.GetCustomAttributes(); foreach (Attribute propertyAttribute in propertyAttributes) { propertyBuilder.HasAnnotation(propertyAttribute); if (propertyAttribute is ColumnAttribute) { ColumnAttribute columnAttribute = (ColumnAttribute)propertyAttribute; if (!string.IsNullOrEmpty(columnAttribute.Name)) { propertyBuilder.MapTo(columnAttribute.Name); } if (columnAttribute.HasDbType()) { propertyBuilder.HasDbType(columnAttribute.DbType); } propertyBuilder.IsPrimaryKey(columnAttribute.IsPrimaryKey); propertyBuilder.IsRowVersion(columnAttribute.IsRowVersion); propertyBuilder.HasSize(columnAttribute.GetSize()); propertyBuilder.HasScale(columnAttribute.GetScale()); propertyBuilder.HasPrecision(columnAttribute.GetPrecision()); continue; } if (propertyAttribute is NotMappedAttribute) { this.Ignore(propertyInfo.Name); continue; } if (propertyAttribute is NotNullAttribute) { propertyBuilder.IsNullable(false); continue; } if (propertyAttribute is AutoIncrementAttribute) { propertyBuilder.IsAutoIncrement(true); continue; } if (propertyAttribute is UpdateIgnoreAttribute) { propertyBuilder.UpdateIgnore(true); continue; } SequenceAttribute sequenceAttribute = propertyAttribute as SequenceAttribute; if (sequenceAttribute != null) { propertyBuilder.HasSequence(sequenceAttribute.Name, sequenceAttribute.Schema); } } } List <PrimitiveProperty> primaryKeys = this.EntityType.PrimitiveProperties.Where(a => a.IsPrimaryKey).ToList(); if (primaryKeys.Count == 0) { //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键 PrimitiveProperty idNameProperty = this.EntityType.PrimitiveProperties.Find(a => string.Equals(a.Property.Name, "id", StringComparison.OrdinalIgnoreCase) && !a.Property.IsDefined(typeof(ColumnAttribute))); if (idNameProperty != null) { this.Property(idNameProperty.Property.Name).IsPrimaryKey(); primaryKeys.Add(idNameProperty); } } if (primaryKeys.Count == 1 && this.EntityType.PrimitiveProperties.Count(a => a.IsAutoIncrement) == 0) { /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */ PrimitiveProperty primaryKey = primaryKeys[0]; if (string.IsNullOrEmpty(primaryKey.SequenceName) && Utils.IsAutoIncrementType(primaryKey.Property.PropertyType) && !primaryKey.Property.IsDefined(typeof(NonAutoIncrementAttribute))) { this.Property(primaryKey.Property.Name).IsAutoIncrement(); } } }
void ConfigureColumnMapping() { var propertyInfos = this.EntityType.Properties.Select(a => a.Property).ToList(); foreach (PropertyInfo propertyInfo in propertyInfos) { IEntityPropertyBuilder propertyBuilder = this.Property(propertyInfo.Name); propertyBuilder.IsPrimaryKey(false); propertyBuilder.IsAutoIncrement(false); var propertyAttributes = propertyInfo.GetCustomAttributes(); foreach (Attribute propertyAttribute in propertyAttributes) { if (propertyAttribute is NotMappedAttribute) { this.Ignore(propertyInfo.Name); } propertyBuilder.HasAnnotation(propertyAttribute); if (propertyAttribute is ColumnAttribute) { ColumnAttribute columnAttribute = (ColumnAttribute)propertyAttribute; if (!string.IsNullOrEmpty(columnAttribute.Name)) { propertyBuilder.MapTo(columnAttribute.Name); } propertyBuilder.IsPrimaryKey(columnAttribute.IsPrimaryKey); propertyBuilder.HasDbType(columnAttribute.GetDbType()); propertyBuilder.HasSize(columnAttribute.GetSize()); propertyBuilder.HasScale(columnAttribute.GetScale()); propertyBuilder.HasPrecision(columnAttribute.GetPrecision()); } if (propertyAttribute is AutoIncrementAttribute) { propertyBuilder.IsAutoIncrement(); } if (propertyAttribute is SequenceAttribute) { propertyBuilder.HasSequence((propertyAttribute as SequenceAttribute).Name); } } } List <EntityProperty> primaryKeys = this.EntityType.Properties.Where(a => a.IsPrimaryKey).ToList(); if (primaryKeys.Count == 0) { //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键 EntityProperty idNameProperty = this.EntityType.Properties.Find(a => a.Property.Name.ToLower() == "id" && !a.Property.IsDefined(typeof(ColumnAttribute))); if (idNameProperty != null) { idNameProperty.IsPrimaryKey = true; primaryKeys.Add(idNameProperty); } } if (primaryKeys.Count == 1 && this.EntityType.Properties.Count(a => a.IsAutoIncrement) == 0) { /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */ EntityProperty primaryKey = primaryKeys[0]; if (Utils.IsAutoIncrementType(primaryKey.Property.PropertyType) && !primaryKey.Property.IsDefined(typeof(NonAutoIncrementAttribute))) { primaryKey.IsAutoIncrement = true; } } }