/// <summary> /// Constructor. /// </summary> public EntityMappingRelationship( Type referencedEntityType, PropertyMapping[] referencingKeyProperties, PropertyDescriptor referencingEntityProperty = null) { this.ReferencingKeyProperties = referencingKeyProperties; this.ReferencingEntityProperty = referencingEntityProperty; this.ReferencedEntityType = referencedEntityType; }
internal PropertyMapping Clone(EntityMapping newEntityMapping) { var clonedPropertyMapping = new PropertyMapping(newEntityMapping, this.Descriptor) { _options = _options, _childParentRelationship = _childParentRelationship == null ? null : new PropertyMappingRelationship(_childParentRelationship.ReferencedEntityType, _childParentRelationship.ReferencingPropertyName), _databaseColumnName = this._databaseColumnName, _columnOrder = _columnOrder }; return(clonedPropertyMapping); }
internal PropertyMapping Clone(EntityMapping newEntityMapping) { var clonedPropertyMapping = new PropertyMapping(newEntityMapping, this._order, this.Descriptor); clonedPropertyMapping._options = this._options; clonedPropertyMapping._databaseColumnName = this._databaseColumnName; if (this._relationshipPropertyNames != null) { clonedPropertyMapping._relationshipPropertyNames = new string[this._relationshipPropertyNames.Length]; Array.Copy( this._relationshipPropertyNames, clonedPropertyMapping._relationshipPropertyNames, this._relationshipPropertyNames.Length); } return(clonedPropertyMapping); }
protected PropertyMapping SetPropertyInternal(PropertyMapping propertyMapping) { return this.PropertyMappings[propertyMapping.PropertyName] = propertyMapping; }
protected PropertyMapping SetPropertyInternal(PropertyMapping propertyMapping) { return(this.PropertyMappings[propertyMapping.PropertyName] = propertyMapping); }
protected bool Equals(PropertyMapping other) { return(this.EntityMapping.Equals(other.EntityMapping) && this.PropertyName.Equals(other.PropertyName)); }
protected bool Equals(PropertyMapping other) { return this.EntityMapping.Equals(other.EntityMapping) && this.PropertyName.Equals(other.PropertyName); }
internal PropertyMapping Clone(EntityMapping newEntityMapping) { var clonedPropertyMapping = new PropertyMapping(newEntityMapping, this._order, this.Descriptor); clonedPropertyMapping._options = this._options; clonedPropertyMapping._databaseColumnName = this._databaseColumnName; if (this._relationshipPropertyNames != null) { clonedPropertyMapping._relationshipPropertyNames = new string[this._relationshipPropertyNames.Length]; Array.Copy( this._relationshipPropertyNames, clonedPropertyMapping._relationshipPropertyNames, this._relationshipPropertyNames.Length); } return clonedPropertyMapping; }
/// <summary> /// Sets up an entity property mapping. /// </summary> public virtual void ConfigureEntityPropertyMapping(PropertyMapping propertyMapping) { // set the Id property to be the primary database generated key in case we don't find any orm attributes on the entity or on the properties if(string.Equals(propertyMapping.PropertyName, "id", StringComparison.InvariantCultureIgnoreCase) && !TypeDescriptor.GetAttributes(propertyMapping.EntityMapping.EntityType).OfType<TableAttribute>().Any() && !this.GetEntityProperties(propertyMapping.EntityMapping.EntityType).Any( propDesc => propDesc.Attributes.OfType<Attribute>().Any( propAttr => propAttr is ColumnAttribute || propAttr is KeyAttribute || propAttr is DatabaseGeneratedAttribute))) { propertyMapping.SetPrimaryKey(); propertyMapping.SetDatabaseGenerated(); return; } var propertyAttributes = propertyMapping.Descriptor.Attributes; var columnAttribute = propertyAttributes.OfType<ColumnAttribute>().FirstOrDefault(); var databaseColumnName = columnAttribute?.Name; if (!string.IsNullOrEmpty(databaseColumnName)) { propertyMapping.SetDatabaseColumnName(databaseColumnName); } if (propertyAttributes.OfType<KeyAttribute>().Any()) { propertyMapping.SetPrimaryKey(); } var databaseGeneratedAttributes = propertyAttributes.OfType<DatabaseGeneratedAttribute>(); if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed)) { propertyMapping.SetDatabaseGenerated(); } if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)) { propertyMapping.SetDatabaseGenerated(); propertyMapping.ExcludeFromInserts().ExcludeFromUpdates(); } //ForeignKeyAttribute foreignKey = null; //if (IsSimpleSqlType(property.PropertyType) && allowSetter) //{ // var propMapping = this.SetPropertyInternal(property); //} //else if((foreignKey = propertyAttributes.OfType<ForeignKeyAttribute>().SingleOrDefault())!= null) //{ // ormAttributesDetected = true; // this.SetPropertyInternal(property).SetRelationship( // foreignKey.Name // .Split(',') // .Select(foreignKeyPropName => foreignKeyPropName.Trim()) // .Where(foreignKeyPropName => !string.IsNullOrWhiteSpace(foreignKeyPropName)) // .ToArray()); //} }
/// <summary> /// Registers a property mapping. /// </summary> protected PropertyMapping SetPropertyInternal(PropertyMapping propertyMapping) { Requires.Argument(propertyMapping.EntityMapping==this, nameof(propertyMapping), "Unable to add a property mapping that is not assigned to the current entity mapping"); _propertyMappings.Remove(propertyMapping); _propertyMappings.Add(propertyMapping); _propertyNameMappingsMap[propertyMapping.PropertyName] = propertyMapping; return propertyMapping; }
/// <summary> /// Sets up an entity property mapping. /// </summary> public virtual void ConfigureEntityPropertyMapping(PropertyMapping propertyMapping) { // set the Id property to be the primary database generated key in case we don't find any orm attributes on the entity or on the properties if(string.Equals(propertyMapping.PropertyName, "id", StringComparison.InvariantCultureIgnoreCase) && this.GetEntityAttributes(propertyMapping.EntityMapping.EntityType).Length == 0 && !this.GetEntityProperties(propertyMapping.EntityMapping.EntityType).Any( propDesc => this.GetEntityPropertyAttributes(propertyMapping.EntityMapping.EntityType, propDesc).Any( propAttr => propAttr is ColumnAttribute || propAttr is KeyAttribute || propAttr is DatabaseGeneratedAttribute))) { propertyMapping.SetPrimaryKey(); propertyMapping.ExcludeFromInserts(); propertyMapping.RefreshOnInserts(); return; } var propertyAttributes = this.GetEntityPropertyAttributes(propertyMapping.EntityMapping.EntityType, propertyMapping.Descriptor); var columnAttribute = propertyAttributes.OfType<ColumnAttribute>().FirstOrDefault(); var databaseColumnName = columnAttribute?.Name; if (!string.IsNullOrEmpty(databaseColumnName)) { propertyMapping.SetDatabaseColumnName(databaseColumnName); } if (propertyAttributes.OfType<KeyAttribute>().Any()) { propertyMapping.SetPrimaryKey(); } if (propertyAttributes.OfType<DatabaseGeneratedDefaultValueAttribute>().Any()) { propertyMapping.ExcludeFromInserts(); propertyMapping.RefreshOnInserts(); } var databaseGeneratedAttributes = propertyAttributes.OfType<DatabaseGeneratedAttribute>(); // https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.databasegeneratedoption(v=vs.110).aspx if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)) { propertyMapping.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); } if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed)) { propertyMapping.SetDatabaseGenerated(DatabaseGeneratedOption.Computed); } //ForeignKeyAttribute foreignKey = null; //if (IsSimpleSqlType(property.PropertyType) && allowSetter) //{ // var propMapping = this.SetPropertyInternal(property); //} //else if((foreignKey = propertyAttributes.OfType<ForeignKeyAttribute>().SingleOrDefault())!= null) //{ // ormAttributesDetected = true; // this.SetPropertyInternal(property).SetRelationship( // foreignKey.Name // .Split(',') // .Select(foreignKeyPropName => foreignKeyPropName.Trim()) // .Where(foreignKeyPropName => !string.IsNullOrWhiteSpace(foreignKeyPropName)) // .ToArray()); //} }
/// <summary> /// Sets up an entity property mapping. /// </summary> public virtual void ConfigureEntityPropertyMapping(PropertyMapping propertyMapping) { // set the Id property to be the primary database generated key in case we don't find any orm attributes on the entity or on the properties if(string.Equals(propertyMapping.PropertyName, "id", StringComparison.OrdinalIgnoreCase) && this.GetEntityAttributes(propertyMapping.EntityMapping.EntityType).Length == 0 && !this.GetEntityProperties(propertyMapping.EntityMapping.EntityType).Any( propDesc => this.GetEntityPropertyAttributes(propertyMapping.EntityMapping.EntityType, propDesc).Any( propAttr => propAttr is ColumnAttribute || propAttr is KeyAttribute || propAttr is DatabaseGeneratedAttribute))) { propertyMapping.SetPrimaryKey(); propertyMapping.ExcludeFromInserts(); propertyMapping.RefreshOnInserts(); return; } //|| (propDesc.PropertyType.IsGenericType && typeof(IEnumerable<>).IsAssignableFrom(propDesc.PropertyType.GetGenericTypeDefinition())))) // solve the parent child relationships //if (propertyMapping.Descriptor.PropertyType.IsGenericType // && typeof(IEnumerable<>).IsAssignableFrom(propertyMapping.Descriptor.PropertyType.GetGenericTypeDefinition())) //{ // var referencedType = propertyMapping.Descriptor.PropertyType.GetGenericArguments()[0]; // propertyMapping.SetParentChildRelationship(referencedType); // return; //} var propertyAttributes = this.GetEntityPropertyAttributes(propertyMapping.EntityMapping.EntityType, propertyMapping.Descriptor); var columnAttribute = propertyAttributes.OfType<ColumnAttribute>().FirstOrDefault(); var databaseColumnName = columnAttribute?.Name; if (!string.IsNullOrEmpty(databaseColumnName)) { propertyMapping.SetDatabaseColumnName(databaseColumnName); } // used for matching relationships var databaseColumnOrder = columnAttribute?.Order; if (databaseColumnOrder.HasValue) { propertyMapping.ColumnOrder = databaseColumnOrder.Value; } if (propertyAttributes.OfType<KeyAttribute>().Any()) { propertyMapping.SetPrimaryKey(); } if (propertyAttributes.OfType<DatabaseGeneratedDefaultValueAttribute>().Any()) { propertyMapping.ExcludeFromInserts(); propertyMapping.RefreshOnInserts(); } var databaseGeneratedAttributes = propertyAttributes.OfType<DatabaseGeneratedAttribute>(); // https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.databasegeneratedoption(v=vs.110).aspx if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)) { propertyMapping.SetDatabaseGenerated(DatabaseGeneratedOption.Identity); } if (databaseGeneratedAttributes.Any(dbGenerated => dbGenerated.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed)) { propertyMapping.SetDatabaseGenerated(DatabaseGeneratedOption.Computed); } var foreignKeyAttribute = propertyAttributes.OfType<ForeignKeyAttribute>().FirstOrDefault(); if (foreignKeyAttribute != null) { var referencingTypePropertyDescriptor = TypeDescriptor.GetProperties(propertyMapping.Descriptor.ComponentType) .OfType<PropertyDescriptor>() .Single(propDescriptor => propDescriptor.Name == foreignKeyAttribute.Name); propertyMapping.SetChildParentRelationship(referencingTypePropertyDescriptor.PropertyType, referencingTypePropertyDescriptor.Name); } }
private bool IsChildrenPropertyMapping(PropertyMapping propMapping, Type entityType) { var propertyType = propMapping.Descriptor.PropertyType; // we're looking for IEnumerable<Entity> return propertyType.IsGenericType && !propertyType.IsGenericTypeDefinition && propertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>) && propertyType.GetGenericArguments().Length == 1 && propertyType.GetGenericArguments()[0] == entityType; }
private bool IsParentPropertyMapping(PropertyMapping propMapping, Type entityType) { return propMapping.Descriptor.PropertyType == entityType; }
private Tuple<PropertyMapping, PropertyMapping>[] GetRelationshipLink( IStatementSqlBuilder sourceSqlBuilder, PropertyMapping sourcePropMapping, IStatementSqlBuilder destinationSqlBuilder, PropertyMapping destinationPropMapping) { var sourcePropNames = sourcePropMapping.RelationshipPropertyNames; var destinationPropNames = destinationPropMapping.RelationshipPropertyNames; if (sourcePropNames == null) { throw new InvalidOperationException($"No property was found for the relationship denoted by '{sourcePropMapping.PropertyName}' on '{sourceSqlBuilder.EntityMapping.EntityType.Name}'"); } if (destinationPropNames == null) { throw new InvalidOperationException($"No property was found for the relationship denoted by '{destinationPropMapping.PropertyName}' on '{destinationSqlBuilder.EntityMapping.EntityType.Name}'"); } if (sourcePropNames.Length == 0 && destinationPropNames.Length == 0) { throw new InvalidOperationException($"No properties were found for the relationship between '{sourceSqlBuilder.EntityMapping.EntityType.Name}[{sourcePropMapping.PropertyName}]' and '{destinationSqlBuilder.EntityMapping.EntityType.Name}[{destinationPropMapping.PropertyName}]'"); } if (sourcePropNames.Length == 0) { if (sourceSqlBuilder.KeyProperties.Length != 1) { throw new InvalidOperationException($"Unable to infer the property used in the relationship between '{sourceSqlBuilder.EntityMapping.EntityType.Name}[{sourcePropMapping.PropertyName}]' and '{destinationSqlBuilder.EntityMapping.EntityType.Name}[{destinationPropMapping.PropertyName}]'"); } sourcePropNames = new[] { sourceSqlBuilder.KeyProperties[0].PropertyName }; } if (destinationPropNames.Length == 0) { if (destinationSqlBuilder.KeyProperties.Length != 1) { throw new InvalidOperationException($"Unable to infer the property used in the relationship between '{destinationSqlBuilder.EntityMapping.EntityType.Name}[{destinationPropMapping.PropertyName}]' and '{sourceSqlBuilder.EntityMapping.EntityType.Name}[{sourcePropMapping.PropertyName}]' "); } destinationPropNames = new[] { sourceSqlBuilder.KeyProperties[0].PropertyName }; } if (sourcePropNames.Length != destinationPropNames.Length) { throw new InvalidOperationException("Invalid number of properties used in the relationship between '{sourceSqlBuilder.EntityMapping.EntityType.Name}[{sourcePropMapping.PropertyName}]' and '{destinationSqlBuilder.EntityMapping.EntityType.Name}[{destinationPropMapping.PropertyName}]'"); } return Enumerable.Range(0, sourcePropNames.Length).Select( index => { PropertyMapping sourcePropertyMapping; if (!sourceSqlBuilder.EntityMapping.PropertyMappings.TryGetValue(sourcePropNames[index], out sourcePropertyMapping)) { throw new InvalidOperationException($"Unable to locate property '{sourcePropNames[index]}' on the entity '{sourceSqlBuilder.EntityMapping.EntityType.Name}' for the relationship with '{destinationSqlBuilder.EntityMapping.EntityType.Name}'"); } PropertyMapping destinationPropertyMapping; if (!destinationSqlBuilder.EntityMapping.PropertyMappings.TryGetValue(destinationPropNames[index], out destinationPropertyMapping)) { throw new InvalidOperationException($"Unable to locate property '{destinationPropNames[index]}' on the entity '{destinationSqlBuilder.EntityMapping.EntityType.Name}' for the relationship with '{sourceSqlBuilder.EntityMapping.EntityType.Name}'"); } return new Tuple<PropertyMapping, PropertyMapping>(sourcePropertyMapping, destinationPropertyMapping); }).ToArray(); }