Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ColumnType return the mapped store type even when not set explicitly #8605

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
Expand Down Expand Up @@ -62,6 +61,8 @@ public virtual ConventionSet AddConventions(ConventionSet conventionSet)
conventionSet.ForeignKeyUniquenessConventions.Add(sharedTableConvention);
conventionSet.ForeignKeyOwnershipConventions.Add(sharedTableConvention);

conventionSet.ModelBuiltConventions.Add(new RelationalTypeMappingConvention(Dependencies.TypeMapper));

return conventionSet;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal
{
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class RelationalTypeMappingConvention : IModelConvention
{
private readonly IRelationalTypeMapper _typeMapper;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public RelationalTypeMappingConvention([NotNull] IRelationalTypeMapper typeMapper)
=> _typeMapper = typeMapper;

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual InternalModelBuilder Apply(InternalModelBuilder modelBuilder)
{
foreach (var property in modelBuilder.Metadata.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
{
property.Builder.HasAnnotation(
RelationalAnnotationNames.TypeMapping,
_typeMapper.FindMapping(property),
ConfigurationSource.Convention);
}

return modelBuilder;
}
}
}
5 changes: 5 additions & 0 deletions src/EFCore.Relational/Metadata/RelationalAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,10 @@ public static class RelationalAnnotationNames
/// The name for filter annotations.
/// </summary>
public const string Filter = Prefix + "Filter";

/// <summary>
/// The name for filter annotations.
/// </summary>
public const string TypeMapping = Prefix + "TypeMapping";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

namespace Microsoft.EntityFrameworkCore.Metadata
Expand Down Expand Up @@ -126,7 +127,8 @@ protected virtual bool SetColumnName([CanBeNull] string value)

public virtual string ColumnType
{
get => (string)Annotations.GetAnnotation(RelationalAnnotationNames.ColumnType);
get => (string)Annotations.GetAnnotation(RelationalAnnotationNames.ColumnType)
?? ((RelationalTypeMapping)Annotations.GetAnnotation(RelationalAnnotationNames.TypeMapping))?.StoreType;
[param: CanBeNull] set => SetColumnType(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public static RelationalTypeMapping GetMapping(
Check.NotNull(typeMapper, nameof(typeMapper));
Check.NotNull(property, nameof(property));

var mapping = typeMapper.FindMapping(property);
var mapping = (RelationalTypeMapping)property[RelationalAnnotationNames.TypeMapping]
?? typeMapper.FindMapping(property);

if (mapping != null)
{
return mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;
using Microsoft.EntityFrameworkCore.Utilities;
using Xunit;
Expand Down Expand Up @@ -2011,6 +2015,28 @@ const string query
private const string FileLineEnding = @"
";

[Fact]
public void Can_get_column_types_from_built_model()
{
using (var context = CreateContext())
{
var typeMapper = context.GetService<IRelationalTypeMapper>();

foreach (var property in context.Model.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
{
var columnType = property.Relational().ColumnType;
Assert.NotNull(columnType);

if (property[RelationalAnnotationNames.ColumnType] == null)
{
Assert.Equal(
columnType.ToLowerInvariant(),
typeMapper.FindMapping(property).StoreType.ToLowerInvariant());
}
}
}
}

private string Sql => Fixture.TestSqlLoggerFactory.Sql.Replace(Environment.NewLine, FileLineEnding);

private class ColumnInfo
Expand Down
26 changes: 26 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/BuiltInDataTypesSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Xunit;

namespace Microsoft.EntityFrameworkCore
Expand Down Expand Up @@ -796,5 +800,27 @@ public virtual void Can_insert_and_read_back_all_mapped_data_types_with_precisio
Assert.Equal(101.1m, entity.Decimal);
}
}

[Fact]
public void Can_get_column_types_from_built_model()
{
using (var context = CreateContext())
{
var typeMapper = context.GetService<IRelationalTypeMapper>();

foreach (var property in context.Model.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()))
{
var columnType = property.Relational().ColumnType;
Assert.NotNull(columnType);

if (property[RelationalAnnotationNames.ColumnType] == null)
{
Assert.Equal(
columnType.ToLowerInvariant(),
typeMapper.FindMapping(property).StoreType.ToLowerInvariant());
}
}
}
}
}
}