Skip to content

Commit

Permalink
startup fix, Version 1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
marcwittke committed Oct 4, 2017
1 parent 84fcb2b commit 7233033
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/marcwittke/Backend.Fx</PackageProjectUrl>
<RepositoryUrl>https://github.com/marcwittke/Backend.Fx.git</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<Version>1.5.0</Version>
<Version>1.5.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 6 additions & 3 deletions src/Backend.Fx.EfCorePersistence/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ private void EnsureSearchIndexExistence()
.Select(t => t.GetTypeInfo())
.Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericType && typeof(IFullTextSearchIndex).GetTypeInfo().IsAssignableFrom(t));

foreach (var fullTextSearchIndexType in fullTextSearchIndexTypes)
using (var dbContext = DbContextOptions.CreateDbContext<TDbContext>())
{
IFullTextSearchIndex fullTextSearchIndex = (IFullTextSearchIndex)Activator.CreateInstance(fullTextSearchIndexType.AsType());
fullTextSearchIndex.EnsureIndex(DbContextOptions);
foreach (var fullTextSearchIndexType in fullTextSearchIndexTypes)
{
IFullTextSearchIndex fullTextSearchIndex = (IFullTextSearchIndex)Activator.CreateInstance(fullTextSearchIndexType.AsType());
fullTextSearchIndex.EnsureIndex(dbContext);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Backend.Fx.EfCorePersistence/IFullTextSearchIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface IFullTextSearchIndex
{
void EnsureIndex(DbContextOptions dbContextOptions);
void EnsureIndex(DbContext dbContext);
}
}
39 changes: 18 additions & 21 deletions src/Backend.Fx.EfCorePersistence/Mssql/MsSqlFullTextSearchIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,30 @@ public abstract class MsSqlFullTextSearchIndex<TAggregateRoot> : IFullTextSearch

public abstract Expression<Func<TAggregateRoot, object>>[] IndexedProperties { get; }

public void EnsureIndex(DbContextOptions dbContextOptions)
public void EnsureIndex(DbContext dbContext)
{
using (var dbContext = new DbContext(dbContextOptions))
{
var entityType = dbContext.Model.FindEntityType(typeof(TAggregateRoot));
var relationalEntityTypeAnnotations = entityType.Relational();
string schema = relationalEntityTypeAnnotations.Schema ?? "dbo";
string table = relationalEntityTypeAnnotations.TableName;
var entityType = dbContext.Model.FindEntityType(typeof(TAggregateRoot));
var relationalEntityTypeAnnotations = entityType.Relational();
string schema = relationalEntityTypeAnnotations.Schema ?? "dbo";
string table = relationalEntityTypeAnnotations.TableName;

IEnumerable<string> indexedColumnDefinitions = IndexedProperties
.Select(prop => entityType.FindProperty(GetMemberName(prop)).Relational().ColumnName)
.Select(col => $"{col} Language {mssqlLocaleId}");
IEnumerable<string> indexedColumnDefinitions = IndexedProperties
.Select(prop => entityType.FindProperty(GetMemberName(prop)).Relational().ColumnName)
.Select(col => $"{col} Language {mssqlLocaleId}");

//var primaryKey = entityType.FindPrimaryKey();
//var mutableIndex = entityType.FindIndex(primaryKey.Properties);
//string primaryKeyIndexName = mutableIndex.Relational().Name;
string primaryKeyIndexName = $"PK_{table}";
//var primaryKey = entityType.FindPrimaryKey();
//var mutableIndex = entityType.FindIndex(primaryKey.Properties);
//string primaryKeyIndexName = mutableIndex.Relational().Name;
string primaryKeyIndexName = $"PK_{table}";

string indexedColumnsSqlFragment = string.Join(", ", indexedColumnDefinitions);
string indexedColumnsSqlFragment = string.Join(", ", indexedColumnDefinitions);

dbContext.Database.ExecuteSqlCommand(
$"IF ((SELECT count(*) FROM sys.fulltext_catalogs WHERE name = '{FullTextCatalogName}') = 0) CREATE FULLTEXT CATALOG {FullTextCatalogName}");
dbContext.Database.ExecuteSqlCommand(
$"IF ((SELECT count(*) FROM sys.fulltext_catalogs WHERE name = '{FullTextCatalogName}') = 0) CREATE FULLTEXT CATALOG {FullTextCatalogName}");

dbContext.Database.ExecuteSqlCommand(
$"IF ((SELECT count(*) FROM sys.fulltext_indexes i INNER JOIN sys.tables t ON t.object_id = i.object_id WHERE t.name = '{table}') = 0) " +
$" CREATE FULLTEXT INDEX ON [{schema}].[{table}] ({indexedColumnsSqlFragment}) KEY INDEX {primaryKeyIndexName} ON {FullTextCatalogName}");
}
dbContext.Database.ExecuteSqlCommand(
$"IF ((SELECT count(*) FROM sys.fulltext_indexes i INNER JOIN sys.tables t ON t.object_id = i.object_id WHERE t.name = '{table}') = 0) " +
$" CREATE FULLTEXT INDEX ON [{schema}].[{table}] ({indexedColumnsSqlFragment}) KEY INDEX {primaryKeyIndexName} ON {FullTextCatalogName}");
}

private static string GetMemberName(Expression<Func<TAggregateRoot, object>> exp)
Expand Down

0 comments on commit 7233033

Please sign in to comment.