Repository: dotnet/efcore
File: test/EFCore.SqlServer.FunctionalTests/Migrations/SqlServerMigrationsSqlGeneratorTest.cs (C#)

Code before:
    {
        var ex = Assert.Throws<InvalidOperationException>(() =>
            Generate(
                new AddColumnOperation
                {
                    Name = "TestColumn",
                    Table = "TestTable",
                    ClrType = typeof(System.IO.FileStream), // Unmappable CLR type
                    ColumnType = null,
                    IsNullable = false
                }));

        Assert.Equal(RelationalStrings.UnsupportedTypeForColumn("TestTable", "TestColumn", "FileStream"), ex.Message);
    }

    [ConditionalFact]
    public void Column_type_found_from_default_value_when_clr_type_unmappable()
    {
        Generate(
            new AddColumnOperation
            {
                Name = "TestColumn",
                Table = "TestTable",
                ClrType = typeof(System.IO.FileStream), // Unmappable CLR type
                ColumnType = null,
                DefaultValue = "test string", // String default value should give us nvarchar mapping
                IsNullable = false
            });

        AssertSql(
            """
ALTER TABLE [TestTable] ADD [TestColumn] nvarchar(max) NOT NULL DEFAULT N'test string';
""");
    }

    private static void CreateGotModel(ModelBuilder b)
        => b.HasDefaultSchema("dbo").Entity(
            "Person", pb =>
            {
                pb.ToTable("People");
                pb.Property<string>("FirstName").HasColumnName("First Name");
                pb.Property<string>("LastName").HasColumnName("Last Name");
                pb.Property<string>("Birthplace").HasColumnName("Birthplace");
                pb.Property<string>("Allegiance").HasColumnName("House Allegiance");
                pb.Property<string>("Culture").HasColumnName("Culture");
                pb.HasKey("FirstName", "LastName");
            });
}


Review comment (bug):
Do we really want the behavior to infer the type from the default value when the ClrType is bad? Is there a real-world case where this may actually happen for a good reason? Otherwise I'd expect this to through as ClrType is simply incorrect.

Code after:
    {
        var ex = Assert.Throws<InvalidOperationException>(() =>
            Generate(
                new AddColumnOperation
                {
                    Name = "TestColumn",
                    Table = "TestTable",
                    ClrType = typeof(System.IO.FileStream), // Unmappable CLR type
                    ColumnType = null,
                    IsNullable = false
                }));

        Assert.Equal(RelationalStrings.UnsupportedTypeForColumn("TestTable", "TestColumn", "FileStream"), ex.Message);
    }

    private static void CreateGotModel(ModelBuilder b)
        => b.HasDefaultSchema("dbo").Entity(
            "Person", pb =>
            {
                pb.ToTable("People");
                pb.Property<string>("FirstName").HasColumnName("First Name");
                pb.Property<string>("LastName").HasColumnName("Last Name");
                pb.Property<string>("Birthplace").HasColumnName("Birthplace");
                pb.Property<string>("Allegiance").HasColumnName("House Allegiance");
                pb.Property<string>("Culture").HasColumnName("Culture");
                pb.HasKey("FirstName", "LastName");
            });
}