public void Process(EntityTypeBuilder <T> builder, DataBaseProvider provider, Func <IConfigurationRuleSet, bool> filter = null) { var mappingRuleSets = filter == null ? _rulesFactory.Value : _rulesFactory.Value.Where(filter); foreach (var mappingRuleSet in mappingRuleSets) { foreach (var rule in mappingRuleSet.GetRules <T>()) { var propertyBuilder = builder.Property(rule.PropertyType, rule.PropertyName); if (rule.IsConcurrencyToken) { propertyBuilder = propertyBuilder.IsConcurrencyToken(); } if (rule.IsRequired) { propertyBuilder = propertyBuilder.IsRequired(); } if (rule.MaxLength.HasValue) { propertyBuilder = propertyBuilder.HasMaxLength(rule.MaxLength.Value); } var columnName = rule.HasColumnName ? DbColumnBuilder.CreateWithPristineValue(rule.ColumnName, provider).Build() : DbColumnBuilder.CreateWithNormalizedValue(rule.PropertyName, provider).Build(); propertyBuilder.HasColumnName(columnName); } } }
/// <summary> /// Add property (threadsafe!) /// </summary> public static void AddProperty <TEntity, TProperty>(this EntityTypeBuilder <TEntity> builder, Expression <Func <TEntity, TProperty> > propertyExpression, string columnName, bool required = false, bool concurrencyToken = false, int?maxLength = null) where TEntity : class, IEntityBase, new() { var member = propertyExpression.Body as MemberExpression; var propInfo = member?.Member as PropertyInfo; var provider = EntityBaseConfiguration <TEntity> .GetProvider(); var propertyName = propInfo?.Name; var columnBuilder = string.IsNullOrWhiteSpace(columnName) ? DbColumnBuilder.CreateWithNormalizedValue(propertyName, provider) : DbColumnBuilder.CreateWithPristineValue(columnName, provider); lock (_syncRoot) { // add column if (propInfo?.PropertyType == MultilingualType) { builder.OwnsOne(typeof(MultilingualString), propInfo.Name, l => { var baseColumnName = columnBuilder.Build(); RefineProperty(l.Property(nameof(MultilingualString.Dutch)) .HasColumnName(DbColumnBuilder.CreateWithPristineValue(baseColumnName, provider).AddNormalized(ColumnDutch).Build()), concurrencyToken, required, maxLength); RefineProperty(l.Property(nameof(MultilingualString.French)) .HasColumnName(DbColumnBuilder.CreateWithPristineValue(baseColumnName, provider).AddNormalized(ColumnFrench).Build()), concurrencyToken, required, maxLength); RefineProperty(l.Property(nameof(MultilingualString.English)) .HasColumnName(DbColumnBuilder.CreateWithPristineValue(baseColumnName, provider).AddNormalized(ColumnEnglish).Build()), concurrencyToken, required, maxLength); }); } else { var propertyBuilder = RefineProperty(builder.Property(propertyExpression), concurrencyToken, required, maxLength); var col = columnBuilder.Build(); propertyBuilder.HasColumnName(col); var searchableType = GetSearchableType(propInfo); // add shadow property for searchable fields if (propInfo.PropertyType == typeof(string) && searchableType != null) { // eg. builder.Property<string>("S_LAST_NAME"); var entityName = typeof(TEntity).FullName; var searchableField = DbColumnBuilder.CreateWithNormalizedValue(SearchablePrefix, provider) .AddPristine(col) .Build(); builder.Property <string>(searchableField); if (!_searchableFields.ContainsKey(entityName)) { _searchableFields.Add(entityName, new SortedDictionary <string, SearchableInfo>()); } if (!_searchableFields[entityName].ContainsKey(propertyName)) { _searchableFields[entityName].Add(propertyName, new SearchableInfo((SearchableType)searchableType, searchableField)); } } if (IsMultiLingual(propInfo)) { var entityName = typeof(TEntity).FullName; if (!_multiLingualFields.ContainsKey(entityName)) { _multiLingualFields.Add(entityName, new HashSet <string>()); } if (!_multiLingualFields[entityName].Contains(propertyName)) { _multiLingualFields[entityName].Add(propertyName); } } #region lexicon info //*** Language if (!EntityILanguage && typeof(ILanguage).IsAssignableFrom(typeof(TEntity))) { var properties = typeof(ILanguage).GetProperties(); // enter only info about ILanguage if (properties.Exist(propInfo.Name)) { if (!_lexiconFields[typeof(ILanguage)].ContainsKey(propInfo.Name)) { _lexiconFields[typeof(ILanguage)].Add(propInfo.Name, col); } else { _lexiconFields[typeof(ILanguage)][propInfo.Name] = col; } // Are properties completly defined ? if (properties.Length == _lexiconFields[typeof(ILanguage)].Count) { EntityILanguage = true; } } } //*** Lexicon if (!EntityILexicon && typeof(ILexicon).IsAssignableFrom(typeof(TEntity))) { var properties = typeof(ILexicon).GetProperties(); // enter only info about ILexicon if (properties.Exist(propInfo.Name)) { if (!_lexiconFields[typeof(ILexicon)].ContainsKey(propInfo.Name)) { _lexiconFields[typeof(ILexicon)].Add(propInfo.Name, col); } else { _lexiconFields[typeof(ILexicon)][propInfo.Name] = col; } // Are properties completly defined ? if (typeof(ILexicon).GetProperties().Length == _lexiconFields[typeof(ILexicon)].Count) { EntityILexicon = true; } } } //*** LexiconItem if (!EntityILexiconItem && typeof(ILexiconItem).IsAssignableFrom(typeof(TEntity))) { var properties = typeof(ILexiconItem).GetProperties(); // enter only info about ILexiconItem if (properties.Exist(propInfo.Name)) { if (!_lexiconFields[typeof(ILexiconItem)].ContainsKey(propInfo.Name)) { _lexiconFields[typeof(ILexiconItem)].Add(propInfo.Name, col); } else { _lexiconFields[typeof(ILexiconItem)][propInfo.Name] = col; } // Are properties completly defined ? if (typeof(ILexiconItem).GetProperties().Length == _lexiconFields[typeof(ILexiconItem)].Count) { EntityILexiconItem = true; } } } #endregion lexicon info } } }