/// <summary> /// Maps a many to one relationship /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The property to map</param> /// <param name="mapper">The property mapper</param> private void MapManyToOne(IModelInspector modelInspector, PropertyPath property, IManyToOneMapper mapper) { Type targetEntityType = property.LocalMember.GetPropertyOrFieldType(); Type sourceEntityType = property.GetContainerEntity(modelInspector); MemberInfo member = property.PreviousPath != null ? property.PreviousPath.LocalMember : property.LocalMember; var targetEntityIDProperty = modelInspector.GetIdentifierMember(targetEntityType); var foreignKeyProperty = property.LocalMember; string columnName = null; string foreignKeyName = null; var one = modelInspector.IsOneToOne(property.LocalMember); if (MatchOneToOneComponent(property, modelInspector)) { columnName = namingEngine.ToComponentForeignKeyColumnName(foreignKeyProperty, member, targetEntityIDProperty); foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, member, targetEntityIDProperty); } else { columnName = namingEngine.ToForeignKeyColumnName(property.LocalMember, targetEntityIDProperty); foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, foreignKeyProperty, targetEntityIDProperty); } mapper.Column(columnName); mapper.ForeignKey(foreignKeyName); }
/// <summary> /// Maps a collection of components or entities /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The property to map</param> /// <param name="mapper">The collections mapper</param> private void MapCollection(IModelInspector modelInspector, PropertyPath property, ICollectionPropertiesMapper mapper) { Type sourceType = property.GetContainerEntity(modelInspector); Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType(); var primaryKeyProperty = modelInspector.GetIdentifierMember(sourceType); var foreignKeyProperty = property.LocalMember; string foreignKeyColumnName = null; string foreignKeyName = null; string tableName = null; string schemaName = null; if (modelInspector.IsEntity(targetType)) { // Entity Relationship Mapping if (modelInspector.IsManyToManyItem(property.LocalMember)) { // Many to many foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, sourceType, primaryKeyProperty); tableName = namingEngine.ToManyToManyTableName(sourceType, targetType); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } else { // One to Many foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToForeignKeyName(targetType, sourceType, sourceType, primaryKeyProperty); } } else if (IsElement(targetType)) { // Element mapping foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty); tableName = namingEngine.ToElementTableName(sourceType, targetType, property.LocalMember); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } else { // Component Relationship Mapping foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty); tableName = namingEngine.ToComponentTableName(sourceType, targetType, property.LocalMember); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } // Mapping mapper.Schema(schemaName); mapper.Table(tableName); mapper.Key(k => { k.Column(foreignKeyColumnName); k.ForeignKey(foreignKeyName); }); }
/// <summary> /// Maps a many to many relationship /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The property to map</param> /// <param name="mapper">The property mapper</param> private void MapManyToMany(IModelInspector modelInspector, PropertyPath property, IManyToManyMapper mapper) { Type sourceType = property.LocalMember.DeclaringType; Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType(); var primaryKeyProperty = modelInspector.GetIdentifierMember(targetType); var foreignKeyProperty = property.LocalMember; string foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(targetType, primaryKeyProperty); string foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, targetType, primaryKeyProperty); mapper.Column(foreignKeyColumnName); mapper.ForeignKey(foreignKeyName); }
/// <summary> /// Maps a joined subclass inheritance hierarchy /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="type">The entity type</param> /// <param name="mapper">The joined subclass mapper</param> private void MapJoinedSubclass(IModelInspector modelInspector, Type type, IJoinedSubclassAttributesMapper mapper) { Type entityType = type.UnderlyingSystemType; Type baseType = type.GetBaseTypes().FirstOrDefault(t => IsEntity(t)); string schemaName = namingEngine.ToSchemaName(entityType); string tableName = namingEngine.ToTableName(entityType); var idProperty = modelInspector.GetIdentifierMember(entityType); string foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(baseType, idProperty); string foreignKeyName = namingEngine.ToForeignKeyName(entityType, baseType, entityType, idProperty); // Mapping mapper.Schema(schemaName); mapper.Table(tableName); mapper.Key(k => { k.Column(foreignKeyColumnName); k.ForeignKey(foreignKeyName); }); }
private void MapClass(IModelInspector modelInspector, Type classType, IClassAttributesMapper mapper) { Type entityType = classType.UnderlyingSystemType; string schemaName = namingEngine.ToSchemaName(entityType) ?? mapper.GetSchema(); string tableName = namingEngine.ToTableName(entityType); var idProperty = modelInspector.GetIdentifierMember(entityType); var versionProperty = modelInspector.GetVersionMember(entityType); string primaryKeyColumnName = namingEngine.ToPrimaryKeyColumnName(entityType, idProperty); // Mapping mapper.Schema(schemaName); mapper.Table(tableName); mapper.Id(id => id.Column(primaryKeyColumnName)); // Version mapping if (versionProperty != null) { string versionColumnName = namingEngine.ToColumnName(versionProperty); mapper.Version(versionProperty, m => m.Column(versionColumnName)); } }
/// <summary> /// Maps a many to one relationship /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The property to map</param> /// <param name="mapper">The property mapper</param> private void MapManyToOne(IModelInspector modelInspector, PropertyPath property, IManyToOneMapper mapper) { Type targetEntityType = property.LocalMember.GetPropertyOrFieldType(); Type sourceEntityType = property.GetContainerEntity(modelInspector); MemberInfo member = property.PreviousPath != null ? property.PreviousPath.LocalMember : property.LocalMember; var targetEntityIDProperty = modelInspector.GetIdentifierMember(targetEntityType); var foreignKeyProperty = property.LocalMember; string columnName = null; string foreignKeyName = null; if (MatchOneToOneComponent(property, modelInspector)) { columnName = namingEngine.ToComponentForeignKeyColumnName(foreignKeyProperty, member, targetEntityIDProperty); foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, member, targetEntityIDProperty); } else { columnName = namingEngine.ToForeignKeyColumnName(property.LocalMember, targetEntityIDProperty); foreignKeyName = namingEngine.ToForeignKeyName(sourceEntityType, targetEntityType, foreignKeyProperty, targetEntityIDProperty); } mapper.Column(columnName); mapper.ForeignKey(foreignKeyName); }
/// <summary> /// Maps a collection of components or entities /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The property to map</param> /// <param name="mapper">The collections mapper</param> private void MapCollection(IModelInspector modelInspector, PropertyPath property, ICollectionPropertiesMapper mapper) { Type sourceType = property.GetContainerEntity(modelInspector); Type targetType = property.LocalMember.GetPropertyOrFieldType().DetermineCollectionElementType(); var primaryKeyProperty = modelInspector.GetIdentifierMember(sourceType); var foreignKeyProperty = property.LocalMember; string foreignKeyColumnName = null; string foreignKeyName = null; string tableName = null; string schemaName = null; if (modelInspector.IsEntity(targetType)) { // Entity Relationship Mapping if (modelInspector.IsManyToMany(property.LocalMember)) { // Many to many foreignKeyColumnName = namingEngine.ToManyToManyForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToManyToManyForeignKeyName(sourceType, targetType, sourceType, primaryKeyProperty); tableName = namingEngine.ToManyToManyTableName(sourceType, targetType); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } else { // One to Many foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToForeignKeyName(targetType, sourceType, sourceType, primaryKeyProperty); } } else if (IsElement(targetType)) { // Element mapping foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty); tableName = namingEngine.ToElementTableName(sourceType, targetType, property.LocalMember); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } else { // Component Relationship Mapping foreignKeyColumnName = namingEngine.ToForeignKeyColumnName(sourceType, primaryKeyProperty); foreignKeyName = namingEngine.ToComponentForeignKeyName(targetType, sourceType, foreignKeyProperty, primaryKeyProperty); tableName = namingEngine.ToComponentTableName(sourceType, targetType, property.LocalMember); schemaName = namingEngine.ToSchemaName(sourceType, targetType); } // Mapping mapper.Schema(schemaName); mapper.Table(tableName); mapper.Key(k => { k.Column(foreignKeyColumnName); k.ForeignKey(foreignKeyName); }); }
/// <summary> /// Maps a property according the naming conventions configuration /// </summary> /// <param name="modelInspector">The model inspector</param> /// <param name="property">The class type</param> /// <param name="mapper">The class mapper</param> private void MapClass(IModelInspector modelInspector, Type classType, IClassAttributesMapper mapper) { Type entityType = classType.UnderlyingSystemType; string schemaName = namingEngine.ToSchemaName(entityType) ?? mapper.GetSchema(); string tableName = namingEngine.ToTableName(entityType); var idProperty = modelInspector.GetIdentifierMember(entityType); var versionProperty = modelInspector.GetVersionMember(entityType); string primaryKeyColumnName = namingEngine.ToPrimaryKeyColumnName(entityType, idProperty); // Mapping mapper.Schema(schemaName); mapper.Table(tableName); mapper.Id(id => id.Column(primaryKeyColumnName)); // Version mapping if (versionProperty != null) { string versionColumnName = namingEngine.ToColumnName(versionProperty); mapper.Version(versionProperty, m => m.Column(versionColumnName)); } }