Implementato il supporto per la scoperta e la visualizzazione dello schema del database, inclusa la creazione di provider specifici per SQL Server e l'integrazione con il servizio di connessione al database.
This commit is contained in:
@@ -106,50 +106,22 @@ public class EFCoreDatabaseManager : IDatabaseManager
|
||||
|
||||
public async Task<IDictionary<string, IEnumerable<DbColumnInfo>>> GetDatabaseSchemaAsync()
|
||||
{
|
||||
// Assicurarsi che il contesto sia connesso
|
||||
await _context.Database.OpenConnectionAsync();
|
||||
|
||||
var result = new Dictionary<string, IEnumerable<DbColumnInfo>>();
|
||||
|
||||
// Ottiene tutte le entità dal modello
|
||||
var model = _context.Model;
|
||||
var entityTypes = model.GetEntityTypes().ToList();
|
||||
|
||||
foreach (var entityType in entityTypes)
|
||||
try
|
||||
{
|
||||
var tableName = entityType.GetTableName();
|
||||
var schemaName = entityType.GetSchema();
|
||||
var fullTableName = string.IsNullOrEmpty(schemaName) ? tableName : $"{schemaName}.{tableName}";
|
||||
// Assicurarsi che il contesto sia connesso
|
||||
await _context.Database.OpenConnectionAsync();
|
||||
|
||||
var columns = new List<DbColumnInfo>();
|
||||
// Usa la factory per ottenere il provider appropriato in base al tipo di database
|
||||
var schemaProvider = DatabaseSchemaProviderFactory.CreateProvider(_options.DatabaseType);
|
||||
|
||||
foreach (var property in entityType.GetProperties())
|
||||
{
|
||||
var columnInfo = new DbColumnInfo
|
||||
{
|
||||
Name = property.GetColumnName(),
|
||||
DataType = property.GetColumnType(),
|
||||
IsNullable = property.IsNullable,
|
||||
IsPrimaryKey = property.IsPrimaryKey()
|
||||
};
|
||||
|
||||
// Verifica se è una foreign key
|
||||
var foreignKeys = entityType.GetForeignKeys().Where(fk => fk.Properties.Contains(property));
|
||||
if (foreignKeys.Any())
|
||||
{
|
||||
var fk = foreignKeys.First();
|
||||
columnInfo.IsForeignKey = true;
|
||||
columnInfo.ReferencedTable = fk.PrincipalEntityType.GetTableName();
|
||||
columnInfo.ReferencedColumn = fk.PrincipalKey.Properties.First().GetColumnName();
|
||||
}
|
||||
|
||||
columns.Add(columnInfo);
|
||||
}
|
||||
|
||||
result.Add(fullTableName, columns);
|
||||
// Usa il provider per ottenere lo schema
|
||||
return await schemaProvider.GetDatabaseSchemaAsync(_context.Database.GetConnectionString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Errore nel recupero dello schema del database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user