I am having a problem with getting the Foreign Keys from TableSchema object. I am programmactically creating list of objects that contains DatabaseSchema property and two TableSchemaCollection properties. This list is populated in primary template and then passed to sub-templates to create my code. The tables in the two collections seem to have all of the other information except the ForeignKeys and I am not sure why. I figure I am I must be doing something wrong in the instantiation of the TableSchema object.
Here is my custom object code:
public class DatabaseInfo
{
#region Private Fields
private string _connectionStringName;
private DatabaseSchema _schema = new DatabaseSchema();
private TableSchemaCollection _entityTables = new TableSchemaCollection();
private TableSchemaCollection _lazyLoadedEntityTables = new TableSchemaCollection();
#endregion
#region Public Properties
public string ConnectionStringName
{
get { return _connectionStringName; }
}
public DatabaseSchema Schema
{
get { return _schema; }
}
public TableSchemaCollection EntityTables
{
get { return _entityTables; }
}
public TableSchemaCollection LazyLoadedEntityTables
{
get { return _lazyLoadedEntityTables; }
}
#endregion
#region Constructor
public DatabaseInfo(string connectionStringName, string connectionString, string providerName)
{
IDbSchemaProvider provider;
switch(providerName)
{
case "SqlSchemaProvider":
provider = new SqlSchemaProvider();
break;
case "ADOXSchemaProvider":
provider = new ADOXSchemaProvider();
break;
default:
throw new Exception("Provider not found.");
}
this._connectionStringName = connectionStringName;
this._schema.ConnectionString = connectionString;
this._schema.Provider = provider;
}
public DatabaseInfo(string connectionStringName, string connectionString, IDbSchemaProvider provider)
{
this._connectionStringName = connectionStringName;
this._schema.ConnectionString = connectionString;
this._schema.Provider = provider;
}
#endregion
}
Here is the code where I populate the list of DatabaseInfo objects:
this.DatabaseList = new List<DatabaseInfo>();
DatabaseInfo info;
foreach(_CodeSmith.DataSourceDatabase database in DataSource.Databases)
{
info = new DatabaseInfo(database.ConnectionStringName, database.ConnectionString, database.ProviderType);
foreach(_CodeSmith.DataSourceDatabaseSourceTable sourceTable in database.SourceTables)
{
info.EntityTables.Add(new TableSchema(info.Schema, sourceTable.Name, sourceTable.Owner, DateTime.Now));
}
foreach(_CodeSmith.DataSourceDatabaseLazyLoadedEntityTable lazyLoadTable in database.LazyLoadedEntityTables)
{
info.LazyLoadedEntityTables.Add(new TableSchema(info.Schema, lazyLoadTable.Name, lazyLoadTable.Owner, DateTime.Now));
}
this.DatabaseList.Add(info);
}
Thanks in advance for any advice.
Aaric