Skip to content

Commit

Permalink
Migrations: Handle properties without column type in snapshot
Browse files Browse the repository at this point in the history
Fixes #18178
  • Loading branch information
bricelam committed Oct 9, 2019
1 parent e5f0e02 commit 4a47907
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
Expand Up @@ -12,6 +12,7 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Utilities;

Expand Down Expand Up @@ -440,7 +441,10 @@ protected virtual void GeneratePropertyAnnotations([NotNull] IProperty property,
.Append(".")
.Append(nameof(RelationalPropertyBuilderExtensions.HasColumnType))
.Append("(")
.Append(Code.Literal(property.GetColumnType()))
.Append(
Code.Literal(
property.GetColumnType()
?? Dependencies.RelationalTypeMappingSource.GetMapping(property).StoreType))
.Append(")");

GenerateFluentApiForAnnotation(
Expand Down
7 changes: 4 additions & 3 deletions src/EFCore.Relational/Migrations/Migration.cs
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;

namespace Microsoft.EntityFrameworkCore.Migrations
Expand Down Expand Up @@ -33,10 +33,11 @@ public virtual IModel TargetModel
{
IModel Create()
{
var modelBuilder = new ModelBuilder(new ConventionSet());
var model = new Model();
var modelBuilder = new ModelBuilder(model);
BuildTargetModel(modelBuilder);

return modelBuilder.Model;
return model.FinalizeModel();
}

return _targetModel ??= Create();
Expand Down
53 changes: 53 additions & 0 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Expand Up @@ -14,6 +14,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Design;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down Expand Up @@ -2039,6 +2040,50 @@ public virtual void Property_multiple_annotations_are_stored_in_snapshot()
});
}

[ConditionalFact]
public virtual void Property_without_column_type()
{
var model = new Model();
var modelBuilder = new ModelBuilder(model);

modelBuilder
.HasAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy, SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("Building", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy, SqlServerValueGenerationStrategy.IdentityColumn);
b.HasKey("Id");
b.ToTable("Buildings");
});

Test(
model.FinalizeModel(),
AddBoilerPlate(@"
modelBuilder
.HasAnnotation(""SqlServer:ValueGenerationStrategy"", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity(""Building"", b =>
{
b.Property<int>(""Id"")
.ValueGeneratedOnAdd()
.HasColumnType(""int"")
.HasAnnotation(""SqlServer:ValueGenerationStrategy"", SqlServerValueGenerationStrategy.IdentityColumn);
b.HasKey(""Id"");
b.ToTable(""Buildings"");
});"),
o =>
{
var property = o.FindEntityType("Building").FindProperty("Id");
Assert.Equal("int", property.GetColumnType());
});
}

#endregion

#region HasKey
Expand Down Expand Up @@ -3556,6 +3601,14 @@ protected void Test(Action<ModelBuilder> buildModel, string expectedCode, Action

var model = modelBuilder.FinalizeModel();

Test(model, expectedCode, assert);
}

protected void Test(IModel model, string expectedCode, Action<IModel> assert)
=> Test(model, expectedCode, (m, _) => assert(m));

protected void Test(IModel model, string expectedCode, Action<IModel, IModel> assert)
{
var sqlServerTypeMappingSource = new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
new RelationalTypeMappingSourceDependencies(
Expand Down

0 comments on commit 4a47907

Please sign in to comment.