void InitMemberInfo() { List <MappingMemberDescriptor> mappingMemberDescriptors = this.ExtractMappingMemberDescriptors(); int primaryKeyCount = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).Count(); if (primaryKeyCount > 1) { throw new NotSupportedException(string.Format("The entity type '{0}' can't define multiple primary keys.", this.EntityType.FullName)); } else if (primaryKeyCount == 1) { this._primaryKey = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).First(); } else { //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键 MappingMemberDescriptor idNameMemberDescriptor = mappingMemberDescriptors.Where(a => a.MemberInfo.Name.ToLower() == "id" && !a.IsDefined(typeof(ColumnAttribute))).FirstOrDefault(); if (idNameMemberDescriptor != null) { idNameMemberDescriptor.IsPrimaryKey = true; this._primaryKey = idNameMemberDescriptor; } } List <MappingMemberDescriptor> autoIncrementMemberDescriptors = mappingMemberDescriptors.Where(a => a.IsDefined(typeof(AutoIncrementAttribute))).ToList(); if (autoIncrementMemberDescriptors.Count > 1) { throw new NotSupportedException(string.Format("The entity type '{0}' can not define multiple autoIncrement members.", this.EntityType.FullName)); } else if (autoIncrementMemberDescriptors.Count == 1) { MappingMemberDescriptor autoIncrementMemberDescriptor = autoIncrementMemberDescriptors[0]; if (autoIncrementMemberDescriptor.IsDefined(typeof(NonAutoIncrementAttribute))) { throw new ChloeException(string.Format("Can't define both 'AutoIncrementAttribute' and 'NotAutoIncrementAttribute' on the same mapping member '{0}'.", autoIncrementMemberDescriptor.MemberInfo.Name)); } if (!IsAutoIncrementType(autoIncrementMemberDescriptor.MemberInfoType)) { throw new ChloeException("AutoIncrement member type must be Int16,Int32 or Int64."); } autoIncrementMemberDescriptor.IsAutoIncrement = true; this._autoIncrement = autoIncrementMemberDescriptor; } else { MappingMemberDescriptor defaultAutoIncrementMemberDescriptor = mappingMemberDescriptors.Where(a => a.IsPrimaryKey && IsAutoIncrementType(a.MemberInfoType) && !a.IsDefined(typeof(NonAutoIncrementAttribute))).FirstOrDefault(); if (defaultAutoIncrementMemberDescriptor != null) { defaultAutoIncrementMemberDescriptor.IsAutoIncrement = true; this._autoIncrement = defaultAutoIncrementMemberDescriptor; } } this._mappingMemberDescriptors = new Dictionary <MemberInfo, MappingMemberDescriptor>(mappingMemberDescriptors.Count); foreach (MappingMemberDescriptor mappingMemberDescriptor in mappingMemberDescriptors) { this._mappingMemberDescriptors.Add(mappingMemberDescriptor.MemberInfo, mappingMemberDescriptor); } }
void InitMemberInfo() { List <MappingMemberDescriptor> mappingMemberDescriptors = this.ExtractMappingMemberDescriptors(); List <MappingMemberDescriptor> primaryKeys = mappingMemberDescriptors.Where(a => a.IsPrimaryKey).ToList(); if (primaryKeys.Count == 0) { //如果没有定义任何主键,则从所有映射的属性中查找名为 id 的属性作为主键 MappingMemberDescriptor idNameMemberDescriptor = mappingMemberDescriptors.Find(a => a.MemberInfo.Name.ToLower() == "id" && !a.IsDefined(typeof(ColumnAttribute))); if (idNameMemberDescriptor != null) { idNameMemberDescriptor.IsPrimaryKey = true; primaryKeys.Add(idNameMemberDescriptor); } } this._primaryKeys = primaryKeys.AsReadOnly(); List <MappingMemberDescriptor> autoIncrementMemberDescriptors = mappingMemberDescriptors.Where(a => a.IsDefined(typeof(AutoIncrementAttribute))).ToList(); if (autoIncrementMemberDescriptors.Count > 1) { throw new NotSupportedException(string.Format("The entity type '{0}' can not define multiple auto increment members.", this.EntityType.FullName)); } else if (autoIncrementMemberDescriptors.Count == 1) { MappingMemberDescriptor autoIncrementMemberDescriptor = autoIncrementMemberDescriptors[0]; if (autoIncrementMemberDescriptor.IsDefined(typeof(NonAutoIncrementAttribute))) { throw new ChloeException(string.Format("Can't define both 'AutoIncrementAttribute' and 'NotAutoIncrementAttribute' on the same mapping member '{0}'.", autoIncrementMemberDescriptor.MemberInfo.Name)); } if (!IsAutoIncrementType(autoIncrementMemberDescriptor.MemberInfoType)) { throw new ChloeException("Auto increment member type must be Int16, Int32 or Int64."); } if (autoIncrementMemberDescriptor.IsPrimaryKey && primaryKeys.Count > 1) { /* 自增列不能作为联合主键 */ throw new ChloeException("Auto increment member can not be union key."); } autoIncrementMemberDescriptor.IsAutoIncrement = true; this._autoIncrement = autoIncrementMemberDescriptor; } else if (primaryKeys.Count == 1) { /* 如果没有显示定义自增成员,并且主键只有 1 个,如果该主键满足一定条件,则默认其是自增列 */ MappingMemberDescriptor primaryKeyDescriptor = primaryKeys[0]; if (IsAutoIncrementType(primaryKeyDescriptor.MemberInfoType) && !primaryKeyDescriptor.IsDefined(typeof(NonAutoIncrementAttribute))) { primaryKeyDescriptor.IsAutoIncrement = true; this._autoIncrement = primaryKeyDescriptor; } } this._mappingMemberDescriptors = new Dictionary <MemberInfo, MappingMemberDescriptor>(mappingMemberDescriptors.Count); foreach (MappingMemberDescriptor mappingMemberDescriptor in mappingMemberDescriptors) { this._mappingMemberDescriptors.Add(mappingMemberDescriptor.MemberInfo, mappingMemberDescriptor); } }