/// <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); } }