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

HasConversion to a nullable Type does not seem to work #33645

Closed
YanicHoegger opened this issue Apr 30, 2024 · 2 comments
Closed

HasConversion to a nullable Type does not seem to work #33645

YanicHoegger opened this issue Apr 30, 2024 · 2 comments

Comments

@YanicHoegger
Copy link

Hello

I would like to change a property of double into a double? column. My idea of doing so would be through a converter. But when I run the dotnet ef Migrations add command it still creates a column with nullable: false. How can I make a nullable column through the fluent api? I also tried to set IsRequired(false) but that throws an exception.

Here is the DataContext i was trying:

namespace EfCoreNaN;

public class MyContext : DbContext
{
    public DbSet<Entity> Entities { get; set; }
    
    protected sealed override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlServer("Server=(localdb)\\test;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        
        var valueConverter = new ValueConverter<double, double?>(x => ConvertToProvider(x), x => ConvertFromProvider(x));
        modelBuilder.Entity("EfCoreNaN.Entity")
            .Property(nameof(Entity.NullableValue))
            .HasConversion(valueConverter);

        modelBuilder.Entity("EfCoreNaN.Entity")
            .Property(nameof(Entity.StringValue))
            .HasConversion(new ValueConverter<double, string>(x => x.ToString(CultureInfo.InvariantCulture), x => double.Parse(x)));
    }

    private double ConvertFromProvider(double? value)
    {
        return value ?? double.NaN;
    }

    private static double? ConvertToProvider(double value)
    {
        return double.IsNaN(value) ? null : value;
    }
}

public class Entity
{
    [Key]
    public int Id { get; set; }
    public double NullableValue { get; set; }
    public double StringValue { get; set; }
}

And that is the generated code:

migrationBuilder.CreateTable(
                name: "Entities",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    NullableValue = table.Column<double>(type: "float", nullable: false),
                    StringValue = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Entities", x => x.Id);
                });

EF Core version: 8.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: W10
IDE: Visual Studio 2022 17.8.6 & JetBrains Rider 2024.1.1

Solution as .zip

@ajcvickers
Copy link
Member

Duplicate of #13850. See also #24685.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
@alandryz83
Copy link

Hello

I would like to change a property of double into a double? column. My idea of doing so would be through a converter. But when I run the dotnet ef Migrations add command it still creates a column with nullable: false. How can I make a nullable column through the fluent api? I also tried to set IsRequired(false) but that throws an exception.

Here is the DataContext i was trying:

namespace EfCoreNaN;



public class MyContext : DbContext

{

    public DbSet<Entity> Entities { get; set; }

    

    protected sealed override void OnConfiguring(DbContextOptionsBuilder options)

    {

        options.UseSqlServer("Server=(localdb)\\test;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true");

    }



    protected override void OnModelCreating(ModelBuilder modelBuilder)

    {

        base.OnModelCreating(modelBuilder);

        

        var valueConverter = new ValueConverter<double, double?>(x => ConvertToProvider(x), x => ConvertFromProvider(x));

        modelBuilder.Entity("EfCoreNaN.Entity")

            .Property(nameof(Entity.NullableValue))

            .HasConversion(valueConverter);



        modelBuilder.Entity("EfCoreNaN.Entity")

            .Property(nameof(Entity.StringValue))

            .HasConversion(new ValueConverter<double, string>(x => x.ToString(CultureInfo.InvariantCulture), x => double.Parse(x)));

    }



    private double ConvertFromProvider(double? value)

    {

        return value ?? double.NaN;

    }



    private static double? ConvertToProvider(double value)

    {

        return double.IsNaN(value) ? null : value;

    }

}



public class Entity

{

    [Key]

    public int Id { get; set; }

    public double NullableValue { get; set; }

    public double StringValue { get; set; }

}

And that is the generated code:

migrationBuilder.CreateTable(

                name: "Entities",

                columns: table => new

                {

                    Id = table.Column<int>(type: "int", nullable: false)

                        .Annotation("SqlServer:Identity", "1, 1"),

                    NullableValue = table.Column<double>(type: "float", nullable: false),

                    StringValue = table.Column<string>(type: "nvarchar(max)", nullable: false)

                },

                constraints: table =>

                {

                    table.PrimaryKey("PK_Entities", x => x.Id);

                });

EF Core version: 8.0.4

Database provider: Microsoft.EntityFrameworkCore.SqlServer

Target framework: .NET 8.0

Operating system: W10

IDE: Visual Studio 2022 17.8.6 & JetBrains Rider 2024.1.1

Solution as .zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants