private void AddOneToManyCollection(INavigation navigation, IEntityType navigationType, StringBuilder stringBuilder, string parentAlias, IProperty keyProperty) { var navigationAlias = this.GetNextAlias(parentAlias); #pragma warning disable EF1001 // Internal EF Core API usage. var primaryKeyName = navigationType.FindDeclaredPrimaryKey().Properties[0].GetColumnName(); #pragma warning restore EF1001 // Internal EF Core API usage. stringBuilder.AppendLine(", (") .Append("select array_to_json(array_agg(row_to_json(").Append(navigationAlias).AppendLine("))) from ("); if (navigation.IsMemberOfAggregate()) { this.AddTypeToQuery(navigationType, stringBuilder, navigationAlias); stringBuilder.Append(") ").AppendLine(navigationAlias) .Append("where ").Append(navigationAlias).Append(".\"").Append(keyProperty.Name).Append("\" = ").Append(parentAlias).Append(".\"").Append(primaryKeyName).AppendLine("\""); } else { var primaryKeyProperty = navigationType.FindPrimaryKey().Properties[0]; // It's always one property, usually called "Id" stringBuilder.Append("select \"").Append(primaryKeyProperty.GetColumnName()).AppendLine("\" as \"$ref\""); stringBuilder.Append("from ").Append(navigationType.GetSchema()).Append(".\"").Append(navigationType.GetTableName()).AppendLine("\" ") .Append("where \"").Append(keyProperty.Name).Append("\" = ").Append(parentAlias).Append(".\"").Append(primaryKeyName).AppendLine("\"") .Append(") as ").AppendLine(navigationAlias); } }
protected virtual void GenerateEntityType( [NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder, GenerateEntityTypeOptions options) { Check.NotNull(entityType, nameof(entityType)); Check.NotNull(stringBuilder, nameof(stringBuilder)); if ((options & GenerateEntityTypeOptions.BaseType) != 0) { stringBuilder .Append("builder.Entity(") .Append(_code.Literal(entityType.Name)) .AppendLine(")"); using (stringBuilder.Indent()) { stringBuilder.Append(".BaseType(") .Append(_code.Literal(entityType.BaseType.Name)) .AppendLine(");"); } return; } stringBuilder .Append("builder.Entity(") .Append(_code.Literal(entityType.Name)) .AppendLine(", b =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); using (stringBuilder.Indent()) { if ((options & GenerateEntityTypeOptions.Declared) != 0) { GenerateProperties(entityType.GetDeclaredProperties(), stringBuilder); GenerateKey(entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateIndexes(entityType.GetDeclaredIndexes(), stringBuilder); } if ((options & GenerateEntityTypeOptions.Relationships) != 0) { GenerateForeignKeys(entityType.GetDeclaredForeignKeys(), stringBuilder); } if ((options & GenerateEntityTypeOptions.Declared) != 0) { GenerateEntityTypeAnnotations(entityType, stringBuilder); } } stringBuilder .AppendLine() .AppendLine("});"); } }
protected virtual void GenerateEntityType( [NotNull] string builderName, [NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotEmpty(builderName, nameof(builderName)); Check.NotNull(entityType, nameof(entityType)); Check.NotNull(stringBuilder, nameof(stringBuilder)); stringBuilder .Append(builderName) .Append(".Entity(") .Append(_code.Literal(entityType.Name)) .AppendLine(", b =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); using (stringBuilder.Indent()) { GenerateBaseType(entityType.BaseType, stringBuilder); GenerateTableName(entityType, stringBuilder); GenerateProperties(entityType.GetDeclaredProperties(), stringBuilder); GenerateKeys(entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateIndexes(entityType.GetDeclaredIndexes(), stringBuilder); GenerateEntityTypeAnnotations(entityType, stringBuilder); } stringBuilder .AppendLine() .AppendLine("});"); } }
private void AddOneToManyCollection(IEntityType navigationType, StringBuilder stringBuilder, string parentAlias, IProperty keyProperty) { var navigationAlias = this.GetNextAlias(parentAlias); var primaryKeyName = navigationType.FindDeclaredPrimaryKey().Properties[0].GetColumnName(); stringBuilder.AppendLine(", (") .Append("select array_to_json(array_agg(row_to_json(").Append(navigationAlias).AppendLine("))) from ("); if (this.SelectReferences(parentAlias, navigationType)) { var primaryKeyProperty = navigationType.FindPrimaryKey().Properties[0]; // It's always one property, usually called "Id" stringBuilder.Append("select \"").Append(primaryKeyProperty.GetColumnName()).AppendLine("\" as \"$ref\""); stringBuilder.Append("from ").Append(navigationType.GetSchema()).Append(".\"").Append(navigationType.GetTableName()).AppendLine("\" ") .Append("where \"").Append(keyProperty.Name).Append("\" = ").Append(parentAlias).Append(".\"").Append(primaryKeyName).AppendLine("\"") .Append(") as ").AppendLine(navigationAlias); } else { this.AddTypeToQuery(navigationType, stringBuilder, navigationAlias); stringBuilder.Append(") ").AppendLine(navigationAlias) .Append("where ").Append(navigationAlias).Append(".\"").Append(keyProperty.Name).Append("\" = ").Append(parentAlias).Append(".\"").Append(primaryKeyName).AppendLine("\""); } }
/// <summary> /// Generates code for an <see cref="IEntityType" />. /// </summary> /// <param name="builderName"> The name of the builder variable. </param> /// <param name="entityType"> The entity type. </param> /// <param name="stringBuilder"> The builder code is added to. </param> protected virtual void GenerateEntityType( [NotNull] string builderName, [NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotEmpty(builderName, nameof(builderName)); Check.NotNull(entityType, nameof(entityType)); Check.NotNull(stringBuilder, nameof(stringBuilder)); stringBuilder .Append(builderName) .Append( entityType.HasDefiningNavigation() ? ".OwnsOne(" : ".Entity(") .Append(Code.Literal(entityType.Name)); if (entityType.HasDefiningNavigation()) { stringBuilder .Append(", ") .Append(Code.Literal(entityType.DefiningNavigationName)); } if (builderName.StartsWith("b", StringComparison.Ordinal)) { // ReSharper disable once InlineOutVariableDeclaration var counter = 1; if (builderName.Length > 1 && int.TryParse(builderName.Substring(1, builderName.Length - 1), out counter)) { counter++; } builderName = "b" + (counter == 0 ? "" : counter.ToString()); } else { builderName = "b"; } stringBuilder .Append(", ") .Append(builderName) .AppendLine(" =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); using (stringBuilder.Indent()) { GenerateBaseType(builderName, entityType.BaseType, stringBuilder); GenerateProperties(builderName, entityType.GetDeclaredProperties(), stringBuilder); if (!entityType.HasDefiningNavigation()) { GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); } GenerateIndexes(builderName, entityType.GetDeclaredIndexes(), stringBuilder); GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder); if (entityType.HasDefiningNavigation()) { GenerateRelationships(builderName, entityType, stringBuilder); } GenerateSeedData(entityType.GetProperties(), entityType.GetSeedData(), stringBuilder); } stringBuilder .AppendLine("});"); } }
/// <summary> /// Generates code for an <see cref="IEntityType" />. /// </summary> /// <param name="builderName"> The name of the builder variable. </param> /// <param name="entityType"> The entity type. </param> /// <param name="stringBuilder"> The builder code is added to. </param> protected virtual void GenerateEntityType( [NotNull] string builderName, [NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotEmpty(builderName, nameof(builderName)); Check.NotNull(entityType, nameof(entityType)); Check.NotNull(stringBuilder, nameof(stringBuilder)); var ownership = entityType.FindOwnership(); var ownerNavigation = ownership?.PrincipalToDependent.Name; stringBuilder .Append(builderName) .Append( ownerNavigation != null ? ownership.IsUnique ? ".OwnsOne(" : ".OwnsMany(" : ".Entity(") .Append(Code.Literal(entityType.Name)); if (ownerNavigation != null) { stringBuilder .Append(", ") .Append(Code.Literal(ownerNavigation)); } if (builderName.StartsWith("b", StringComparison.Ordinal)) { // ReSharper disable once InlineOutVariableDeclaration var counter = 1; if (builderName.Length > 1 && int.TryParse(builderName.Substring(1, builderName.Length - 1), out counter)) { counter++; } builderName = "b" + (counter == 0 ? "" : counter.ToString()); } else { builderName = "b"; } stringBuilder .Append(", ") .Append(builderName) .AppendLine(" =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); using (stringBuilder.Indent()) { GenerateBaseType(builderName, entityType.BaseType, stringBuilder); GenerateProperties(builderName, entityType.GetDeclaredProperties(), stringBuilder); GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateIndexes(builderName, entityType.GetDeclaredIndexes(), stringBuilder); GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder); if (ownerNavigation != null) { GenerateRelationships(builderName, entityType, stringBuilder); } GenerateData(builderName, entityType.GetProperties(), entityType.GetData(providerValues: true), stringBuilder); } stringBuilder .AppendLine("});"); } }
protected override void GenerateEntityType( string builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) { var isUserType = entityType.IsUserDefinedType(); var ownership = entityType.FindOwnership(); var ownerNavigation = ownership?.PrincipalToDependent.Name; stringBuilder .Append(builderName) .Append( ownerNavigation != null ? ownership.IsUnique ? ".OwnsOne(" : ".OwnsMany(" : ".Entity(") .Append(Code.Literal(entityType.Name)); if (ownerNavigation != null) { stringBuilder .Append(", ") .Append(ownerNavigation); } if (builderName.StartsWith("b", StringComparison.Ordinal)) { var counter = 1; if (builderName.Length > 1 && int.TryParse(builderName.Substring(1), out counter)) { counter++; } builderName = "b" + (counter == 0 ? "" : counter.ToString()); } else { builderName = "b"; } stringBuilder .Append(", ") .Append(builderName) .AppendLine(" =>"); using (stringBuilder.Indent()) { stringBuilder.Append("{"); var properties = entityType.GetDeclaredProperties(); var assms = AppDomain.CurrentDomain.GetAssemblies(); using (stringBuilder.Indent()) { GenerateBaseType(builderName, entityType.BaseType, stringBuilder); GenerateProperties(builderName, entityType.GetDeclaredProperties().Where(p => CassandraMigrationsModelDiffer.CheckProperty(assms, p)), stringBuilder); GenerateKeys(builderName, entityType.GetDeclaredKeys(), entityType.FindDeclaredPrimaryKey(), stringBuilder); GenerateEntityTypeAnnotations(builderName, entityType, stringBuilder); GenerateCheckConstraints(builderName, entityType, stringBuilder); if (ownerNavigation != null) { GenerateRelationships(builderName, entityType, stringBuilder); } GenerateData(builderName, entityType.GetProperties(), entityType.GetSeedData(providerValues: true), stringBuilder); } stringBuilder .AppendLine("});"); } }