Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected override MigrationStatement Generate(AddForeignKeyOperation op)
- {
- StringBuilder sb = new StringBuilder();
- string fkName = string.Format(
- CultureInfo.InvariantCulture,
- "FK_{0}_{1}_{2}",
- TrimSchemaPrefix(op.DependentTable),
- TrimSchemaPrefix(op.PrincipalTable),
- op.DependentColumns.Join(separator: "_"));
- if (fkName.Length > 64)
- {
- MD5 md5 = System.Security.Cryptography.MD5.Create();
- fkName = "FK_" + md5.CalculateMD5Hash(string.Format(
- CultureInfo.InvariantCulture,
- "{0}_{1}_{2}",
- TrimSchemaPrefix(op.DependentTable),
- TrimSchemaPrefix(op.PrincipalTable),
- op.DependentColumns.Join(separator: "_")));
- }
- sb.Append("alter table `" + TrimSchemaPrefix(op.DependentTable) + "` add constraint `" + TrimSchemaPrefix(fkName) + "` " +
- " foreign key ");
- sb.Append("(" + string.Join(",", op.DependentColumns.Select(c => "`" + c + "`")) + ") ");
- sb.Append("references `" + TrimSchemaPrefix(op.PrincipalTable) + "` ( " + string.Join(",", op.PrincipalColumns.Select(c => "`" + c + "`")) + ") ");
- if (op.CascadeDelete)
- {
- sb.Append(" on update cascade on delete cascade ");
- }
- return new MigrationStatement { Sql = sb.ToString() };
- }
- protected override MigrationStatement Generate(DropForeignKeyOperation op)
- {
- StringBuilder sb = new StringBuilder();
- string fkName = string.Format(
- CultureInfo.InvariantCulture,
- "FK_{0}_{1}_{2}",
- TrimSchemaPrefix(op.DependentTable),
- TrimSchemaPrefix(op.PrincipalTable),
- op.DependentColumns.Join(separator: "_"));
- if (fkName.Length > 64)
- {
- MD5 md5 = System.Security.Cryptography.MD5.Create();
- fkName = "FK_" + md5.CalculateMD5Hash(string.Format(
- CultureInfo.InvariantCulture,
- "{0}_{1}_{2}",
- TrimSchemaPrefix(op.DependentTable),
- TrimSchemaPrefix(op.PrincipalTable),
- op.DependentColumns.Join(separator: "_")));
- }
- sb = sb.AppendFormat("alter table `{0}` drop foreign key `{1}`", op.DependentTable, fkName);
- return new MigrationStatement { Sql = sb.ToString() };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement