private void BuildRefConstraints() { foreach (var table in _dbModel.Tables) { if (table.Kind != EntityKind.Table) { continue; } var entity = table.Entity; foreach (var member in entity.Members) { if (member.Kind != MemberKind.EntityRef) { continue; } var refInfo = member.ReferenceInfo; if (!IsActive(refInfo.ToKey.Entity.Area)) { continue; //target entity/table is not in this db model; so no foreign key link } var fromDbKey = _dbModel.LookupDbObject <DbKeyInfo>(refInfo.FromKey); var toDbKey = _dbModel.LookupDbObject <DbKeyInfo>(refInfo.ToKey); var cascadeDelete = member.Flags.IsSet(EntityMemberFlags.CascadeDelete); var dbRC = new DbRefConstraintInfo(_dbModel, fromDbKey, toDbKey, cascadeDelete, refInfo); table.RefConstraints.Add(dbRC); } }//foreach entityInfo CheckErrors(); }
// public virtual void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint) { BuildTableConstraintDropSql(change, dbRefConstraint.FromKey); }
private void BuildRefConstraints() { foreach (var table in _dbModel.Tables) { if (table.Kind != EntityKind.Table) continue; var entity = table.Entity; foreach (var member in entity.Members) { if (member.Kind != MemberKind.EntityRef) continue; var refInfo = member.ReferenceInfo; if (!IsActive(refInfo.ToKey.Entity.Area)) continue; //target entity/table is not in this db model; so no foreign key link var fromDbKey = _dbModel.LookupDbObject<DbKeyInfo>(refInfo.FromKey); var toDbKey = _dbModel.LookupDbObject<DbKeyInfo>(refInfo.ToKey); var cascadeDelete = member.Flags.IsSet(EntityMemberFlags.CascadeDelete); var dbRC = new DbRefConstraintInfo(_dbModel, fromDbKey, toDbKey, cascadeDelete, refInfo); table.RefConstraints.Add(dbRC); } }//foreach entityInfo CheckErrors(); }
//not supported public override void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint) { }
public virtual void BuildRefConstraintAddSql(DbObjectChange change, DbRefConstraintInfo refConstraint) { const string sqlTemplate = "ALTER TABLE {0} ADD CONSTRAINT \"{1}\" FOREIGN KEY ({2}) REFERENCES {3} ({4}) {5};"; var srcTable = refConstraint.FromKey.Table; var targetTable = refConstraint.ToKey.Table; var fullSrcTableRef = srcTable.FullName; var fullTargetTableRef = targetTable.FullName; var srcCols = refConstraint.FromKey.KeyColumns.GetSqlNameList(); var targetCols = refConstraint.ToKey.KeyColumns.GetSqlNameList(); bool cascade = refConstraint.OwnerReference.FromMember.Flags.IsSet(EntityMemberFlags.CascadeDelete); var onDeleteClause = cascade ? "ON DELETE CASCADE" : string.Empty; change.AddScript(DbScriptType.RefConstraintAdd, sqlTemplate, fullSrcTableRef, refConstraint.FromKey.Name, srcCols, fullTargetTableRef, targetCols, onDeleteClause); }
public override void BuildRefConstraintAddSql(DbObjectChange change, DbRefConstraintInfo refConstraint) { }
protected virtual void LoadReferentialConstraints() { var data = GetReferentialConstraintsExt(); foreach (DataRow row in data.Rows) { //Load names for Foreign key and Unique key var fkSchema = row.GetAsString("CONSTRAINT_SCHEMA"); var toSchema = row.GetAsString("UNIQUE_CONSTRAINT_SCHEMA"); if (!IncludeSchema(toSchema) || !IncludeSchema(fkSchema)) continue; var fkTableName = row.GetAsString("C_TABLE"); var fkName = row.GetAsString("CONSTRAINT_NAME"); var fromKey = FindKey(fkSchema, fkTableName, fkName); Util.Check(fromKey != null, "Key {0} for table {1} not found.", fkName, fkTableName); var toTableName = row.GetAsString("U_TABLE"); var toTable = Model.GetTable(toSchema, toTableName); Util.Check(toTable != null, "Target table {0}.{1} not found in DB Model.", toSchema, toTableName); var toKey = toTable.PrimaryKey; /* if (toKey == null) { //catch special case - foreign key to non-key set of fields if (fromKey != null && fromKey.KeyType.IsSet(KeyType.ForeignKey)) fromKey.NotSupported = true; continue; //it is not ref constraint } */ bool cascadeDelete = row.GetAsString("DELETE_RULE") == "CASCADE"; var refConstraint = new DbRefConstraintInfo(Model, fromKey, toKey, cascadeDelete); fromKey.Table.RefConstraints.Add(refConstraint); } }
private bool RefConstraintKeysChanged(DbRefConstraintInfo constraint) { return _changedKeys.Contains(constraint.FromKey) || _changedKeys.Contains(constraint.ToKey); }
private DbRefConstraintInfo FindOldRefConstraint(DbTableInfo oldTable, DbRefConstraintInfo newRefConstraint) { var newFrom = newRefConstraint.FromKey; var newTo = newRefConstraint.ToKey; foreach (var oldRc in oldTable.RefConstraints) if (oldRc.FromKey.Peer == newFrom && oldRc.ToKey.Peer == newTo && oldRc.CascadeDelete == newRefConstraint.CascadeDelete) return oldRc; return null; }
public override void BuildRefConstraintDropSql(DbObjectChange change, DbRefConstraintInfo dbRefConstraint) { var fromKey = dbRefConstraint.FromKey; change.AddScript(DbScriptType.RefConstraintDrop, "ALTER TABLE {0} DROP FOREIGN KEY {1};", fromKey.Table.FullName, fromKey.Name); }