本文整理汇总了C#中IGenerator.GenerateForeignKeys方法的典型用法代码示例。如果您正苦于以下问题:C# IGenerator.GenerateForeignKeys方法的具体用法?C# IGenerator.GenerateForeignKeys怎么用?C# IGenerator.GenerateForeignKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IGenerator
的用法示例。
在下文中一共展示了IGenerator.GenerateForeignKeys方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDiffScript
public static void CreateDiffScript(IRepository sourceRepository, IRepository targetRepository,IGenerator generator, bool includeTargetDrops)
{
List<string> sourceTables = sourceRepository.GetAllTableNames();
List<string> targetTables = targetRepository.GetAllTableNames();
// Script each table not in the target
foreach (string tableName in sourceTables.Except(targetTables))
{
generator.GenerateTableCreate(tableName);
}
foreach (string tableName in sourceTables.Except(targetTables))
{
generator.GeneratePrimaryKeys(tableName);
}
foreach (string tableName in sourceTables.Except(targetTables))
{
List<string> tableIndexes = sourceRepository.GetIndexesFromTable(tableName).Select(i => i.IndexName).Distinct().ToList();
foreach (var index in tableIndexes)
{
generator.GenerateIndexScript(tableName, index);
}
}
foreach (string tableName in sourceTables.Except(targetTables))
{
generator.GenerateForeignKeys(tableName);
}
// Drop each table in the target but not the source
if (includeTargetDrops)
{
foreach (string tableName in targetTables.Except(sourceTables))
{
generator.GenerateTableDrop(tableName);
}
}
//For each table both in target and source
foreach (string tableName in sourceTables.Intersect(targetTables))
{
// Check columns for the table: Dropped, added or changed ?
IEnumerable<Column> sourceColumns = from c in sourceRepository.GetColumnsFromTable()
where c.TableName == tableName
select c;
IEnumerable<Column> targetColumns = from c in targetRepository.GetColumnsFromTable()
where c.TableName == tableName
select c;
// Added columns
foreach (var column in sourceColumns.Except(targetColumns, new ColumnComparer()))
{
generator.GenerateColumnAddScript(column);
}
// Same columns, check for changes
foreach (var sourceColumn in sourceColumns.Intersect(targetColumns, new ColumnComparer()))
{
bool altered = false;
// Check if they have any differences:
var targetColumn = (from c in targetColumns
where c.TableName == sourceColumn.TableName && c.ColumnName == sourceColumn.ColumnName
select c).Single();
if (sourceColumn.IsNullable != targetColumn.IsNullable)
altered = true;
if (sourceColumn.NumericPrecision != targetColumn.NumericPrecision)
altered = true;
if (sourceColumn.NumericScale != targetColumn.NumericScale)
altered = true;
if (sourceColumn.AutoIncrementBy != targetColumn.AutoIncrementBy)
altered = true;
if (sourceColumn.CharacterMaxLength != targetColumn.CharacterMaxLength)
altered = true;
if (sourceColumn.DataType != targetColumn.DataType)
altered = true;
if (altered)
generator.GenerateColumnAlterScript(sourceColumn);
// Changed defaults is special case
if (!targetColumn.ColumnHasDefault && sourceColumn.ColumnHasDefault)
{
generator.GenerateColumnSetDefaultScript(sourceColumn);
}
if (!sourceColumn.ColumnHasDefault && targetColumn.ColumnHasDefault)
{
generator.GenerateColumnDropDefaultScript(sourceColumn);
}
// If both columns have defaults, but they are different
if ((sourceColumn.ColumnHasDefault && targetColumn.ColumnHasDefault) && (sourceColumn.ColumnDefault != targetColumn.ColumnDefault))
{
generator.GenerateColumnSetDefaultScript(sourceColumn);
}
}
//Check primary keys
List<PrimaryKey> sourcePK = sourceRepository.GetAllPrimaryKeys().Where(p => p.TableName == tableName).ToList();
List<PrimaryKey> targetPK = targetRepository.GetAllPrimaryKeys().Where(p => p.TableName == tableName).ToList();
// Add the PK
if (targetPK.Count == 0 && sourcePK.Count > 0)
{
generator.GeneratePrimaryKeys(tableName);
//.........这里部分代码省略.........