Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ protected virtual void GeneratePropertyAnnotations(
GenerateFluentApiForPrecisionAndScale(property, stringBuilder);
GenerateFluentApiForIsUnicode(property, stringBuilder);

if (!annotations.ContainsKey(RelationalAnnotationNames.ColumnType))
if (!annotations.ContainsKey(RelationalAnnotationNames.ColumnType)
&& !property.DeclaringType.IsMappedToJson())
{
annotations[RelationalAnnotationNames.ColumnType] = new Annotation(
RelationalAnnotationNames.ColumnType,
Expand Down Expand Up @@ -568,7 +569,8 @@ protected virtual void GenerateComplexProperty(
stringBuilder
.AppendLine()
.Append(builderName)
.Append($".ComplexProperty<{Code.Reference(Model.DefaultPropertyBagType)}>(")
.Append(complexProperty.IsCollection ? ".ComplexCollection(" : ".ComplexProperty(")
.Append($"typeof({Code.Reference(Model.DefaultPropertyBagType)}), ")
.Append($"{Code.Literal(complexProperty.Name)}, {Code.Literal(complexType.Name)}, ")
.Append(complexTypeBuilderName)
.AppendLine(" =>");
Expand All @@ -579,7 +581,7 @@ protected virtual void GenerateComplexProperty(

using (stringBuilder.Indent())
{
if (complexProperty.IsNullable != complexProperty.ClrType.IsNullableType())
if (!complexProperty.IsNullable)
{
stringBuilder
.AppendLine()
Expand Down Expand Up @@ -680,8 +682,8 @@ protected virtual void GenerateComplexPropertyAnnotations(
}

var propertyAnnotations = Dependencies.AnnotationCodeGenerator
.FilterIgnoredAnnotations(property.GetAnnotations())
.ToDictionary(a => a.Name, a => a);
.FilterIgnoredAnnotations(property.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

var typeAnnotations = Dependencies.AnnotationCodeGenerator
.FilterIgnoredAnnotations(property.ComplexType.GetAnnotations())
Expand All @@ -692,7 +694,7 @@ protected virtual void GenerateComplexPropertyAnnotations(
inChainedCall: false, hasAnnotationMethodInfo: HasPropertyAnnotationMethodInfo);

GenerateAnnotations(
propertyBuilderName, property, stringBuilder, typeAnnotations,
propertyBuilderName, property.ComplexType, stringBuilder, typeAnnotations,
inChainedCall: false, hasAnnotationMethodInfo: HasTypeAnnotationMethodInfo);
}

Expand Down
46 changes: 46 additions & 0 deletions src/EFCore.Relational/Design/AnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

#pragma warning disable EF1001 // Accessing annotation names (internal)
Expand Down Expand Up @@ -253,6 +254,18 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
annotations.Remove(RelationalAnnotationNames.ContainerColumnType);
}

if (annotations.TryGetValue(RelationalAnnotationNames.JsonPropertyName, out var jsonPropertyNameAnnotation)
&& jsonPropertyNameAnnotation is { Value: string jsonPropertyName }
&& entityType.IsOwned())
{
methodCallCodeFragments.Add(
new MethodCallCodeFragment(
nameof(RelationalOwnedNavigationBuilderExtensions.HasJsonPropertyName),
jsonPropertyName));

annotations.Remove(RelationalAnnotationNames.JsonPropertyName);
}

methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(entityType, annotations, GenerateFluentApi));

return methodCallCodeFragments;
Expand All @@ -265,6 +278,31 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var methodCallCodeFragments = new List<MethodCallCodeFragment>();

if (annotations.TryGetValue(RelationalAnnotationNames.ContainerColumnName, out var containerColumnNameAnnotation)
&& containerColumnNameAnnotation is { Value: string containerColumnName })
{
methodCallCodeFragments.Add(
new MethodCallCodeFragment(
nameof(RelationalComplexPropertyBuilderExtensions.ToJson),
containerColumnName));

annotations.Remove(RelationalAnnotationNames.ContainerColumnName);
#pragma warning disable CS0618
annotations.Remove(RelationalAnnotationNames.ContainerColumnTypeMapping);
#pragma warning restore CS0618
}

if (annotations.TryGetValue(RelationalAnnotationNames.ContainerColumnType, out var containerColumnTypeAnnotation)
&& containerColumnTypeAnnotation is { Value: string containerColumnType })
{
methodCallCodeFragments.Add(
new MethodCallCodeFragment(
nameof(RelationalComplexPropertyBuilderExtensions.HasColumnType),
containerColumnType));

annotations.Remove(RelationalAnnotationNames.ContainerColumnType);
}

methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(complexType, annotations, GenerateFluentApi));

return methodCallCodeFragments;
Expand Down Expand Up @@ -336,6 +374,10 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
: new MethodCallCodeFragment(nameof(RelationalPropertyBuilderExtensions.IsFixedLength), false));
}

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.JsonPropertyName, nameof(RelationalPropertyBuilderExtensions.HasJsonPropertyName), methodCallCodeFragments);

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.Comment, nameof(RelationalPropertyBuilderExtensions.HasComment), methodCallCodeFragments);
Expand All @@ -356,6 +398,10 @@ public virtual IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
{
var methodCallCodeFragments = new List<MethodCallCodeFragment>();

GenerateSimpleFluentApiCall(
annotations,
RelationalAnnotationNames.JsonPropertyName, nameof(RelationalComplexPropertyBuilderExtensions.HasJsonPropertyName), methodCallCodeFragments);

methodCallCodeFragments.AddRange(GenerateFluentApiCallsHelper(complexProperty, annotations, GenerateFluentApi));

return methodCallCodeFragments;
Expand Down
Loading
Loading