/// <summary> /// creates a description of all table/full text indices from the model /// </summary> /// <param name="dbCtx"></param> /// <param name="datbaseName"></param> /// <returns></returns> private static Dictionary <string, string> DescribeAllFullTextIndices(DbContext dbCtx) { bool lowerCaseTableNames = dbCtx.MySqlLowerCaseTableNames(); Dictionary <string, string> returnValue = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (IEntityType entityType in dbCtx.Model.GetEntityTypes().Where(ent => ent.ClrType.GetCustomAttribute <OwnedAttribute>() == null).ToList()) { string tableName = lowerCaseTableNames ? entityType.Relational().TableName.ToLowerInvariant() : entityType.Relational().TableName; string indexName = dbCtx.GetFullTextIndexName(entityType.ClrType); string keyName = tableName + ":" + indexName; Dictionary <string, FullTextAttribute> fullTextMap = new Dictionary <string, FullTextAttribute>(StringComparer.OrdinalIgnoreCase); foreach (PropertyInfo pInfo in entityType.ClrType.GetProperties()) { FullTextAttribute ftAttribute = pInfo.GetCustomAttributes <FullTextAttribute>().FirstOrDefault() as FullTextAttribute; if (null != ftAttribute) { string columnName = pInfo.Name; ColumnAttribute columnNameAttribute = pInfo.GetCustomAttributes <ColumnAttribute>().FirstOrDefault() as ColumnAttribute; if (null != columnNameAttribute && !columnNameAttribute.Name.IsNullOrEmpty()) { columnName = columnNameAttribute.Name; } fullTextMap[columnName] = ftAttribute; } } if (fullTextMap.Count > 0) { List <string> columnNames = SortByColumnOrder(fullTextMap); returnValue[keyName] = string.Join(",", columnNames); } } return(returnValue); }
/// <summary> /// Gets the full text index name of a table /// </summary> /// <param name="dbCtx"></param> /// <param name="entityType"></param> /// <returns></returns> public static string GetFullTextIndexName(this DbContext dbCtx, Type entityType) { string tableName = dbCtx.GetActualTableName(entityType); string returnValue = "FT_" + tableName; FullTextAttribute ftAttribute = entityType.GetCustomAttributes <FullTextAttribute>().FirstOrDefault() as FullTextAttribute; if (null != ftAttribute && !ftAttribute.Name.IsNullOrWhiteSpace()) { returnValue = ftAttribute.Name; } return(returnValue); }
/// <summary> /// creates full text indices /// </summary> /// <param name="dbCtx"></param> public static void CreateFullTextIndices(this DbContext dbCtx) { string databaseName = dbCtx.DatabaseName(); bool lowerCaseTableNames = dbCtx.MySqlLowerCaseTableNames(); Dictionary <string, Dictionary <string, FullTextAttribute> > fullTextMap = new Dictionary <string, Dictionary <string, FullTextAttribute> >(StringComparer.OrdinalIgnoreCase); foreach (IEntityType entityType in dbCtx.Model.GetEntityTypes().Where(ent => ent.ClrType.GetCustomAttribute <OwnedAttribute>() == null).ToList()) { string tableName = lowerCaseTableNames ? entityType.Relational().TableName.ToLowerInvariant() : entityType.Relational().TableName; string indexName = dbCtx.GetFullTextIndexName(entityType.ClrType); tableName = tableName + ":" + indexName; foreach (PropertyInfo pInfo in entityType.ClrType.GetProperties()) { FullTextAttribute ftAttribute = pInfo.GetCustomAttributes <FullTextAttribute>().FirstOrDefault() as FullTextAttribute; if (null != ftAttribute) { string columnName = pInfo.Name; ColumnAttribute columnNameAttribute = pInfo.GetCustomAttributes <ColumnAttribute>().FirstOrDefault() as ColumnAttribute; if (null != columnNameAttribute && !columnNameAttribute.Name.IsNullOrEmpty()) { columnName = columnNameAttribute.Name; } Dictionary <string, FullTextAttribute> tableMap = null; if (!fullTextMap.TryGetValue(tableName, out tableMap)) { tableMap = new Dictionary <string, FullTextAttribute>(StringComparer.OrdinalIgnoreCase); fullTextMap[tableName] = tableMap; } tableMap[columnName] = ftAttribute; } } } DropFullTextIndices(dbCtx, databaseName); if (fullTextMap.Count > 0) { List <string> sqlCommands = GenerateCreateFullTextIndexCommands(dbCtx, fullTextMap); foreach (string sqlCommand in sqlCommands) { dbCtx.ExecuteSqlCommand(sqlCommand); } } }