private void GenerateForeignKeyAttribute(INavigation navigation) { if (navigation.IsOnDependent) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { // TODO: Resolve TransformNavPropertyName() method NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(navigation); var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); if (navigation.ForeignKey.Properties.Count > 1) { foreignKeyAttribute.AddParameter( CSharpHelper.Literal( string.Join(",", navigation.ForeignKey.Properties.Select(p => EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, p.Name, p.ClrType.Name, navEntityPropertyInfo.PropertyIsNullable))))); } else { foreignKeyAttribute.AddParameter($"nameof({EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, navigation.ForeignKey.Properties.First().Name, navigation.ForeignKey.Properties.First().ClrType.Name, navEntityPropertyInfo.PropertyIsNullable)})"); } NavPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", foreignKeyAttribute } }); } } }
private void GenerateInversePropertyAttribute(INavigation navigation) { if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey()) { var inverseNavigation = navigation.Inverse; if (inverseNavigation != null) { // TODO: Resolve TransformNavPropertyName() method NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(navigation); var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); var propertyName = EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, inverseNavigation.Name, navigation.DeclaringType.Name, navEntityPropertyInfo.PropertyIsNullable); inversePropertyAttribute.AddParameter( !navigation.DeclaringEntityType.GetPropertiesAndNavigations().Any( m => m.Name == inverseNavigation.DeclaringEntityType.Name || EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, m.Name, navigation.TargetEntityType.Name, navEntityPropertyInfo.PropertyIsNullable) == EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, inverseNavigation.DeclaringEntityType.Name, navigation.TargetEntityType.Name, navEntityPropertyInfo.PropertyIsNullable)) ? $"nameof({EntityTypeTransformationService.TransformTypeEntityName(inverseNavigation.DeclaringType.Name)}.{propertyName})" : CSharpHelper.Literal(propertyName)); NavPropertyAnnotations.Add(new Dictionary <string, object> { { "nav-property-annotation", inversePropertyAttribute } }); } } }
/// <summary> /// Generate entity type navigation properties. /// </summary> /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param> protected override void GenerateNavigationProperties(IEntityType entityType) { Check.NotNull(entityType, nameof(entityType)); var sortedNavigations = entityType.GetNavigations() .OrderBy(n => n.IsOnDependent ? 0 : 1) .ThenBy(n => n.IsCollection ? 1 : 0); if (sortedNavigations.Any()) { var navProperties = new List <Dictionary <string, object> >(); foreach (var navigation in sortedNavigations) { // TODO: Resolve TransformNavPropertyName() method NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(navigation); navProperties.Add(new Dictionary <string, object> { { "foregin-entity-name", navEntityPropertyInfo.ForeginEntityName }, { "field-name", navEntityPropertyInfo.FieldName }, { "nav-property-collection", navigation.IsCollection }, { "nav-property-type", navigation.TargetEntityType.Name }, { "nav-property-name", TypeScriptHelper.ToCamelCase(navigation.Name) }, { "nav-property-annotations", new List <Dictionary <string, object> >() }, { "nullable-reference-types", _options?.Value?.EnableNullableReferenceTypes == true } }); } var transformedNavProperties = EntityTypeTransformationService.TransformNavigationProperties(entityType.Name, navProperties); TemplateData.Add("nav-properties", transformedNavProperties); } }
/// <summary> /// Generate entity type navigation properties. /// </summary> /// <param name="entityType">Represents an entity type in an <see cref="T:Microsoft.EntityFrameworkCore.Metadata.IModel" />.</param> protected override void GenerateNavigationProperties(IEntityType entityType) { Check.NotNull(entityType, nameof(entityType)); var sortedNavigations = entityType.GetScaffoldNavigations(_options.Value) .OrderBy(n => n.IsOnDependent ? 0 : 1) .ThenBy(n => n.IsCollection ? 1 : 0); if (sortedNavigations.Any()) { var navProperties = new List <Dictionary <string, object> >(); foreach (var navigation in sortedNavigations) { NavPropertyAnnotations = new List <Dictionary <string, object> >(); // TODO: Resolve TransformNavPropertyName() method NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(navigation); if (UseDataAnnotations) { GenerateNavigationDataAnnotations(navigation); } var propertyIsNullable = !navigation.IsCollection && ( navigation.IsOnDependent ? !navigation.ForeignKey.IsRequired : !navigation.ForeignKey.IsRequiredDependent ); var navPropertyType = navigation.TargetEntityType.Name; if (_options?.Value?.EnableNullableReferenceTypes == true && !navPropertyType.EndsWith("?") && propertyIsNullable) { navPropertyType += "?"; } navProperties.Add(new Dictionary <string, object> { { "foregin-entity-name", navEntityPropertyInfo.ForeginEntityName }, { "field-name", navEntityPropertyInfo.FieldName }, { "nav-property-collection", navigation.IsCollection }, { "nav-property-type", navPropertyType }, { "nav-property-name", navigation.Name }, { "nav-property-annotations", NavPropertyAnnotations }, { "nav-property-isnullable", propertyIsNullable }, { "nullable-reference-types", _options?.Value?.EnableNullableReferenceTypes == true } }); } var transformedNavProperties = EntityTypeTransformationService.TransformNavigationProperties(entityType.Name, navProperties); TemplateData.Add("nav-properties", transformedNavProperties); } }
/// <summary> /// /// </summary> /// <param name="entityType"></param> /// <param name="foreignKey"></param> /// <returns></returns> public NavEntityPropertyInfo ResolvingName(IEntityType entityType, IForeignKey foreignKey) { NavEntityPropertyInfo item = new NavEntityPropertyInfo(); item.FieldName = foreignKey.Properties[0].Name; item.ForeginEntityName = foreignKey.PrincipalEntityType.Name; item.EntityName = entityType.Name; item.PropertyIsNullable = !foreignKey.IsRequired; return(item); }
/// <summary> /// /// </summary> /// <param name="navigation"></param> /// <returns></returns> public NavEntityPropertyInfo ResolvingName(INavigation navigation) { NavEntityPropertyInfo item = new NavEntityPropertyInfo(); item.FieldName = navigation.ForeignKey.PrincipalEntityType.Name != navigation.DeclaringEntityType.Name ? navigation.ForeignKey.Properties[0].Name : ""; item.ForeginEntityName = navigation.ForeignKey.PrincipalEntityType.Name != navigation.DeclaringEntityType.Name ? navigation.ForeignKey.PrincipalEntityType.Name : navigation.ForeignKey.DeclaringEntityType.Name; item.EntityName = navigation.DeclaringEntityType.Name; item.PropertyIsNullable = navigation.ForeignKey.PrincipalEntityType.Name != navigation.DeclaringEntityType.Name ? !navigation.ForeignKey.IsRequired : true; return(item); }
/// <summary> /// Transform entity type navigation properties. /// </summary> /// <param name="entityName">Entity type navigation properties.</param> /// <param name="navProperties">Entity type navigation properties.</param> /// <returns>Transformed entity type navigation properties.</returns> public List <Dictionary <string, object> > TransformNavigationProperties(string entityName, List <Dictionary <string, object> > navProperties) { var transformedNavProperties = new List <Dictionary <string, object> >(); foreach (var navProperty in navProperties) { var propTypeInfo = new NavEntityPropertyInfo( navProperty["field-name"] as string, navProperty["foregin-entity-name"] as string, entityName, navProperty["nav-property-type"] as string, navProperty["nav-property-name"] as string); var transformedProp = NavPropertyTransformer?.Invoke(propTypeInfo) ?? propTypeInfo; var newNavProperty = new Dictionary <string, object>(navProperty); newNavProperty["nav-property-type"] = transformedProp.PropertyType; newNavProperty["nav-property-name"] = transformedProp.PropertyName; transformedNavProperties.Add(newNavProperty); } return(transformedNavProperties); }
/// <summary> /// Transform single navigation property name. /// </summary> /// <param name="fieldName">Property name.</param> /// <param name="foreginEntityName">Property name.</param> /// <param name="entityName">Property name.</param> /// <param name="propertyType">Property type</param> /// <param name="propertyName">Property type</param> /// <param name="propertyIsNullable">Property type</param> /// <returns>Transformed property name.</returns> public string TransformNavPropertyName(string fieldName, string foreginEntityName, string entityName, string propertyType, string propertyName, bool propertyIsNullable = false) { var propTypeInfo = new NavEntityPropertyInfo(fieldName, foreginEntityName, entityName, propertyType, propertyName, propertyIsNullable); return(NavPropertyTransformer?.Invoke(propTypeInfo)?.PropertyName ?? propertyName); }
private void GenerateRelationship(IEntityType entityType, IForeignKey foreignKey, bool useDataAnnotations, IndentedStringBuilder sb) { var canUseDataAnnotations = true; var annotations = AnnotationCodeGenerator .FilterIgnoredAnnotations(foreignKey.GetAnnotations()) .ToDictionary(a => a.Name, a => a); AnnotationCodeGenerator.RemoveAnnotationsHandledByConventions(foreignKey, annotations); // TODO: Resolve TransformNavPropertyName() method NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(entityType, foreignKey); var lines = new List <string> { $".{nameof(EntityTypeBuilder.HasOne)}(" + (foreignKey.DependentToPrincipal != null ? $"d => d.{EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName,foreignKey.DependentToPrincipal?.Name, foreignKey.PrincipalToDependent?.DeclaringType.Name, navEntityPropertyInfo.PropertyIsNullable)}" : null) + ")", $".{(foreignKey.IsUnique ? nameof(ReferenceNavigationBuilder.WithOne) : nameof(ReferenceNavigationBuilder.WithMany))}" + "(" + (foreignKey.PrincipalToDependent != null ? $"p => p.{EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, foreignKey.PrincipalToDependent?.Name, foreignKey.DependentToPrincipal?.DeclaringType.Name, navEntityPropertyInfo.PropertyIsNullable)}" : null) + ")" }; if (!foreignKey.PrincipalKey.IsPrimaryKey()) { Func <string, string, string> tmpNameTransformFunction = (tmpName, tmpType) => { return(EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, tmpName, tmpType, navEntityPropertyInfo.PropertyIsNullable)); }; canUseDataAnnotations = false; lines.Add( $".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}" + (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(((ITypeBase)foreignKey.PrincipalEntityType).DisplayName(), "")}>" : "") + $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p", tmpNameTransformFunction)})"); } lines.Add( $".{nameof(ReferenceReferenceBuilder.HasForeignKey)}" + (foreignKey.IsUnique ? $"<{GetEntityTypeName(foreignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(((ITypeBase)foreignKey.DeclaringEntityType).DisplayName()))}>" : "") + $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d", EntityTypeTransformationService.TransformPropertyName)})"); var defaultOnDeleteAction = foreignKey.IsRequired ? DeleteBehavior.Cascade : DeleteBehavior.ClientSetNull; if (foreignKey.DeleteBehavior != defaultOnDeleteAction) { canUseDataAnnotations = false; lines.Add( $".{nameof(ReferenceReferenceBuilder.OnDelete)}({CSharpHelper.Literal(foreignKey.DeleteBehavior)})"); } if (!string.IsNullOrEmpty((string)foreignKey[RelationalAnnotationNames.Name])) { canUseDataAnnotations = false; } lines.AddRange( AnnotationCodeGenerator.GenerateFluentApiCalls(foreignKey, annotations).Select(m => CSharpHelper.Fragment(m)) .Concat(GenerateAnnotations(annotations.Values))); if (!useDataAnnotations || !canUseDataAnnotations) { AppendMultiLineFluentApi(foreignKey.DeclaringEntityType, lines, sb); } }