/// <summary> /// Returns the name of the table column for an entity's property. /// </summary> /// <param name="inspector">The model inspector.</param> /// <param name="member">The entity property.</param> /// <param name="declaringType"> </param> /// <returns>The name of the table column.</returns> public string Column(IModelInspector inspector, PropertyPath member, Type declaringType = null) { Requires.That(member, "member").IsNotNull(); Requires.That(member.LocalMember, "member.LocalMember").IsNotNull(); var localName = member.LocalMember.Name.Underscore(); if (declaringType != null) { return(KeyColumnFormat.Formatted(declaringType.Name.Underscore(), localName)); } var type = member.LocalMember.GetPropertyOrFieldType(); if (inspector.IsEntity(type)) // is a foreign key { var id = inspector.FindPersistentId(type); if (id != null) { return(KeyColumnFormat.Formatted(localName, Column(inspector, id))); } } return(localName); }
public static System.Type GetContainerEntity(this PropertyPath propertyPath, IModelInspector domainInspector) { PropertyPath analizing = propertyPath; while (analizing.PreviousPath != null && !domainInspector.IsEntity(analizing.LocalMember.ReflectedType)) { analizing = analizing.PreviousPath; } return analizing.LocalMember.ReflectedType; }
/// <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); }); }
public static System.Type GetContainerEntity(this PropertyPath propertyPath, IModelInspector domainInspector) { PropertyPath analizing = propertyPath; while (analizing.PreviousPath != null && !domainInspector.IsEntity(analizing.LocalMember.ReflectedType)) { analizing = analizing.PreviousPath; } return(analizing.LocalMember.ReflectedType); }
/// <summary> /// Returns the name of the table column for an entity's property. /// </summary> /// <param name="inspector">The model inspector.</param> /// <param name="member">The entity property.</param> /// <param name="declaringType"> </param> /// <returns>The name of the table column.</returns> public string Column(IModelInspector inspector, PropertyPath member, Type declaringType = null) { Requires.That(member, "member").IsNotNull(); Requires.That(member.LocalMember, "member.LocalMember").IsNotNull(); var localName = member.LocalMember.Name.Underscore(); if (declaringType != null) { return KeyColumnFormat.Formatted(declaringType.Name.Underscore(), localName); } var type = member.LocalMember.GetPropertyOrFieldType(); if (inspector.IsEntity(type)) // is a foreign key { var id = inspector.FindPersistentId(type); if (id != null) return KeyColumnFormat.Formatted(localName, Column(inspector, id)); } return localName; }
/// <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); }); }