diff --git a/src/FluentMigrator.T4/FM.Core.ttinclude b/src/FluentMigrator.T4/FM.Core.ttinclude index 3c6b8b0db..56b2c3f18 100644 --- a/src/FluentMigrator.T4/FM.Core.ttinclude +++ b/src/FluentMigrator.T4/FM.Core.ttinclude @@ -116,7 +116,9 @@ public class FKey { public string ToTable; public string FromColumn; - public string ToColumn; + public string ToColumn; + public bool CascadeDelete; + public bool CascadeUpdate; } @@ -737,8 +739,28 @@ class SqlServerSchemaReader : SchemaReader List LoadFKeys(string tblName) { - var result=new List(); - return result; + using (var cmd = _factory.CreateCommand()) + { + cmd.Connection = _connection; + cmd.CommandText = string.Format(FKEY_INFO_SQL); + cmd.Parameters.Add(new SqlParameter("@tblName", tblName)); + + var result = new List(); + using (var rdr = cmd.ExecuteReader()) + { + while (rdr.Read()) + { + var key = new FKey(); + key.ToTable = (string)rdr["Table"]; + key.ToColumn = (string)rdr["To"]; + key.FromColumn = (string)rdr["From"]; + key.CascadeDelete = (bool)rdr["CascadeDelete"]; + key.CascadeUpdate = (bool)rdr["CascadeUpdate"]; + result.Add(key); + } + } + return result; + } } string GetPK(string table){ @@ -847,7 +869,25 @@ class SqlServerSchemaReader : SchemaReader FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@tableName AND TABLE_SCHEMA=@schemaName ORDER BY OrdinalPosition ASC"; - + + const string FKEY_INFO_SQL = @"SELECT + columnsA.name AS [From] , tablesB.name AS [Table], columnsB.name AS [To] + , cast((case when (fk.delete_referential_action_desc = 'CASCADE') then 1 else 0 end) as bit) as [CascadeDelete] + , cast((case when (fk.update_referential_action_desc = 'CASCADE') then 1 else 0 end) as bit) as [CascadeUpdate] + FROM sys.foreign_key_columns fkc + JOIN sys.columns AS columnsA + ON fkc.parent_object_id = columnsA.object_id + AND fkc.parent_column_id = columnsA.column_id + JOIN sys.columns AS columnsB + ON fkc.referenced_object_id = columnsB.object_id + AND fkc.referenced_column_id = columnsB.column_id + JOIN sys.tables AS tablesA + ON fkc.parent_object_id = tablesA.object_id + JOIN sys.tables AS tablesB + ON fkc.referenced_object_id = tablesB.object_id + JOIN sys.foreign_keys fk + ON fkc.constraint_object_id = fk.object_id + WHERE tablesA.object_id = OBJECT_ID(@tblName)"; } class SqlServerCeSchemaReader : SchemaReader diff --git a/src/FluentMigrator.T4/IntialMigrationCode.tt b/src/FluentMigrator.T4/IntialMigrationCode.tt index 00d20b31f..84975b2a2 100644 --- a/src/FluentMigrator.T4/IntialMigrationCode.tt +++ b/src/FluentMigrator.T4/IntialMigrationCode.tt @@ -50,6 +50,7 @@ if (string.IsNullOrEmpty(Namespace)) Namespace=ConnectionStringName; if (string.IsNullOrEmpty(Namespace)) Namespace="FluentMigration"; #> using System; +using System.Data; using System.Collections.Generic; using System.Linq; using System.Web; @@ -68,15 +69,12 @@ foreach(Table tbl in from t in tables where !t.Ignore select t) { #> //For <#=tbl.Name#> - Create.Table("<#=tbl.Name#>")<#if (string.IsNullOrEmpty(tbl.Schema) == false ){#>.InSchema("<#=tbl.Schema#>")<#}#> -<# + Create.Table("<#=tbl.Name#>")<#if (string.IsNullOrEmpty(tbl.Schema) == false ){#>.InSchema("<#=tbl.Schema#>")<#} var ColumnList = from c in tbl.Columns where !c.Ignore select c; -foreach(Column col in ColumnList) -{ -#> - .WithColumn("<#=col.Name#>").<#=GetMigrationTypeFunctionForType(col.PropertyType,col.Size,col.Precision)#><#if (col.IsPK){#>.PrimaryKey()<#}#><#if (col.IsAutoIncrement){#>.Identity()<#}#><#if (col.IsNullable){#>.Nullable()<#} else {#>.NotNullable()<#}#><#if (col.DefaultValue != null){#>.WithDefaultValue(<#= GetColumnDefaultValue(col) #>)<#}#> -<# -} + foreach(Column col in ColumnList) + { #> + .WithColumn("<#=col.Name#>").<#=GetMigrationTypeFunctionForType(col.PropertyType,col.Size,col.Precision)#><#if (col.IsPK){#>.PrimaryKey()<#}#><#if (col.IsAutoIncrement){#>.Identity()<#}#><#if (col.IsNullable){#>.Nullable()<#} else {#>.NotNullable()<#}#><#if (col.DefaultValue != null){#>.WithDefaultValue(<#= GetColumnDefaultValue(col) #>)<#} + } #> ; <# @@ -109,10 +107,19 @@ foreach(Table tbl in from t in tables where !t.Ignore select t) List FKeyList = tbl.FKeys; foreach(FKey fkey in FKeyList) { -#> - <#if(commentAlreadyWritten == false) { #>//Foreign Key List <# commentAlreadyWritten = true;} #> - Create.ForeignKey().FromTable("<#=tbl.Name#>").ForeignColumn("<#=fkey.FromColumn#>").ToTable("<#=fkey.ToTable#>").PrimaryColumn("<#=fkey.ToColumn#>") - ; + if(commentAlreadyWritten == false) { #> + // Foreign Key List <# commentAlreadyWritten = true;} #> + Create.ForeignKey() + .FromTable("<#=tbl.Name#>").ForeignColumn("<#=fkey.FromColumn#>") + .ToTable("<#=fkey.ToTable#>").PrimaryColumn("<#=fkey.ToColumn#>")<# + if (fkey.CascadeDelete) + { #> + .OnDelete(Rule.Cascade)<# + } + if (fkey.CascadeUpdate) + { #> + .OnUpdate(Rule.Cascade)<# } + #>; <# } }