/// <summary> /// The transform index. /// </summary> /// <param name="index"> /// The index. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public static string TransformIndex(Index index, INamingConvention nc = null) { var idxs = new StringBuilder("HasIndex(i => new {"); bool isClustered = index.IndexType == IndexType.Clustered; if (index.Columns != null && index.Columns.Any()) { for (int i = 0; i < index.Columns.Count; i++) { if (i > 0) { idxs.Append(", "); } string colName = nc != null?nc.ApplyNamingConvention(index.Columns[i]) : index.Columns[i]; idxs.Append($"i.{colName}"); } } idxs.Append("})"); idxs.Append($".HasName(\"{index.Name}\").IsUnique({index.IsUnique.ToString().ToLower()})"); return(idxs.ToString()); }
/// <summary> /// The transform relationship. /// </summary> /// <param name="table"> /// The table. /// </param> /// <param name="rel"> /// The rel. /// </param> /// <param name="models"> /// The models. /// </param> /// <param name="relationships"> /// The relationships. /// </param> /// <param name="nc"> /// The nc. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string TransformRelationship( string table, Relationship rel, IEnumerable <Table> models, IEnumerable <Relationship> relationships = null, INamingConvention nc = null) { var sb = new StringBuilder(); string refTableName = RelationshipNameFormatting.FormatName( rel.ReferencedTableName, rel.RelationshipAlias, nc); string parentTableName = nc != null?nc.ApplyNamingConvention(table) : table; if (relationships != null && relationships.Any()) { var parentRels = (from tbl in models select tbl.Relationships.FirstOrDefault( o => o.ReferencedTableName == table && o.Table.TableName == rel.ReferencedTableName && o.SchemaName == rel.SchemaName && o.ColumnName == rel.ReferencedColumnName && o.ReferencedColumnName == rel.ColumnName)).Where(x => x != null); var parentRel = parentRels.FirstOrDefault(); if (parentRel != null) { parentTableName = RelationshipNameFormatting.FormatName( parentRel.ReferencedTableName, parentRel.RelationshipAlias, nc); } } if (rel.Multiplicity == RelationshipMultiplicity.Many) { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $"HasManyToMany(o => o.{refTableName});" : $"References(o => o.{refTableName}).Column(\"{rel.ReferencedColumnName}\").Unique().Not.Insert().Not.Update();"); } else if (rel.Multiplicity == RelationshipMultiplicity.One) { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $"HasMany(s => s.{refTableName}).KeyColumn(\"{rel.ReferencedColumnName}\");" : $"HasOne(s => s.{refTableName}).PropertyRef(o => o.{parentTableName});"); } else { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $"HasMany(s => s.{refTableName}).KeyColumn(\"{rel.ReferencedColumnName}\");" : $"HasOne(s => s.{refTableName});"); } return(sb.ToString()); }
/// <summary> /// The transform model name. /// </summary> /// <param name="name"> /// The name. /// </param> /// <param name="nc"> /// The nc. /// </param> /// <returns> /// The <see cref="string" />. /// </returns> public string TransformModelName(string name, INamingConvention nc = null) { if (nc == null) { return(name); } return(nc.ApplyNamingConvention(name)); }
private string ApplyNamingConvention(INamingConvention nc, string name) { return(nc != null?nc.ApplyNamingConvention(name) : name); }
/// <summary> /// The transform relationship. /// </summary> /// <param name="table"> /// The table. /// </param> /// <param name="rel"> /// The rel. /// </param> /// <param name="models"> /// The models. /// </param> /// <param name="relationships"> /// The relationships. /// </param> /// <param name="nc"> /// The nc. /// </param> /// <returns> /// The <see cref="string" />. /// </returns> public string TransformRelationship( string table, Relationship rel, IEnumerable <Table> models, IEnumerable <Relationship> relationships = null, INamingConvention nc = null) { var sb = new StringBuilder(); string refTableName = RelationshipNameFormatting.FormatName( rel.ReferencedTableName, rel.RelationshipAlias, nc); if (rel.ReferencedMultiplicity == RelationshipMultiplicity.Many) { sb.Append($"HasMany<{TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{refTableName})"); } else if (rel.ReferencedMultiplicity == RelationshipMultiplicity.One) { sb.Append( $"HasRequired<{TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{refTableName})"); } else { sb.Append( $"HasOptional<{TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{refTableName})"); } bool referencedRelExists = true; string parentTableName = nc != null?nc.ApplyNamingConvention(table) : table; if (relationships != null && relationships.Any()) { var parentRels = (from tbl in models select tbl.Relationships.FirstOrDefault( o => o.ReferencedTableName == table && o.Table.TableName == rel.ReferencedTableName && o.SchemaName == rel.SchemaName && o.ColumnName == rel.ReferencedColumnName && o.ReferencedColumnName == rel.ColumnName)).Where(x => x != null); var parentRel = parentRels.FirstOrDefault(); referencedRelExists = parentRel != null; if (parentRel != null) { parentTableName = RelationshipNameFormatting.FormatName( parentRel.ReferencedTableName, parentRel.RelationshipAlias, nc); } } if (rel.Multiplicity == RelationshipMultiplicity.Many) { if (referencedRelExists) { sb.Append( $".WithMany(s => s.{parentTableName}).HasForeignKey(s => s.{ApplyNamingConvention(nc, rel.ColumnName)}).WillCascadeOnDelete(false);"); } else { if (rel.ColumnRequired) { sb.Append(".WithRequiredDependent();"); } else { if (rel.ReferencedColumnRequired) { sb.Append(".WithOptional();"); } else { sb.Append(".WithOptionalDependent();"); } } } } else if (rel.Multiplicity == RelationshipMultiplicity.One) { string multiplicityText = string.Empty; switch (rel.ReferencedMultiplicity) { case RelationshipMultiplicity.ZeroToOne: multiplicityText = $".WithRequired(s => s.{parentTableName}).WillCascadeOnDelete(false);"; break; case RelationshipMultiplicity.One: multiplicityText = $".WithRequiredPrincipal(s => s.{parentTableName}).WillCascadeOnDelete(false);"; break; case RelationshipMultiplicity.Many: multiplicityText = $".WithRequired(s => s.{parentTableName}).HasForeignKey(s => s.{ApplyNamingConvention(nc, rel.ReferencedColumnName)}).WillCascadeOnDelete(false);"; break; } sb.Append(multiplicityText); } else { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $".WithOptional(s => s.{parentTableName}).HasForeignKey(s => s.{ApplyNamingConvention(nc, rel.ReferencedColumnName)}).WillCascadeOnDelete(false);" : $".WithOptional(s => s.{parentTableName}).WillCascadeOnDelete(false);"); } return(sb.ToString()); }
/// <summary> /// The transform relationship. /// </summary> /// <param name="table"> /// The table. /// </param> /// <param name="rel"> /// The rel. /// </param> /// <param name="models"> /// The models. /// </param> /// <param name="relationships"> /// The relationships. /// </param> /// <param name="nc"> /// The nc. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string TransformRelationship( string table, Relationship rel, IEnumerable <Table> models, IEnumerable <Relationship> relationships = null, INamingConvention nc = null) { var sb = new StringBuilder(); string refTableName = RelationshipNameFormatting.FormatName( rel.ReferencedTableName, rel.RelationshipAlias, nc); string parentTableName = nc != null?nc.ApplyNamingConvention(table) : table; if (relationships != null && relationships.Any()) { var parentRels = (from tbl in models select tbl.Relationships.FirstOrDefault( o => o.ReferencedTableName == table && o.Table.TableName == rel.ReferencedTableName && o.SchemaName == rel.SchemaName && o.ColumnName == rel.ReferencedColumnName && o.ReferencedColumnName == rel.ColumnName)).Where(x => x != null); var parentRel = parentRels.FirstOrDefault(); if (parentRel != null) { parentTableName = RelationshipNameFormatting.FormatName( parentRel.ReferencedTableName, parentRel.RelationshipAlias, nc); } } sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $"builder.HasMany<{this.TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{refTableName})" : $"builder.HasOne<{this.TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{refTableName})"); if (rel.Multiplicity == RelationshipMultiplicity.Many) { sb.Append( $".WithMany(s => s.{parentTableName}).HasForeignKey(s => s.{rel.ColumnName}).OnDelete(DeleteBehavior.Restrict);"); } else if (rel.Multiplicity == RelationshipMultiplicity.One) { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $".WithOne(s => s.{parentTableName}).HasForeignKey(s => s.{rel.ReferencedColumnName}).OnDelete(DeleteBehavior.Restrict);" : $".WithOne(s => s.{parentTableName}).HasForeignKey<{this.TransformModelName(rel.ReferencedTableName, nc)}>(s => s.{rel.ReferencedColumnName}).OnDelete(DeleteBehavior.Restrict);"); } else { sb.Append( rel.ReferencedMultiplicity == RelationshipMultiplicity.Many ? $".WithOne(s => s.{parentTableName}).HasForeignKey(s => s.{rel.ReferencedColumnName}).OnDelete(DeleteBehavior.Restrict);" : $".WithOne(s => s.{parentTableName}).OnDelete(DeleteBehavior.Restrict);"); } return(sb.ToString()); }
public static string FormatName(string name, INamingConvention nc = null) { return(nc != null?nc.ApplyNamingConvention(name) : name); }