ErikEJ / EFCorePowerTools

Entity Framework Core Power Tools - reverse engineering, migrations and model visualization in Visual Studio & CLI
MIT License
2.18k stars 298 forks source link

NotNull varchar column being mapped to property with default value of Null #2503

Closed Germs2004 closed 1 month ago

Germs2004 commented 1 month ago

Provide steps to reproduce a bug

If I create a not-null varchar column in my table, I notice EF Core Power Tools maps that to a not-null "string" datatype, but also sets its default value to "null!". If the column is supposed to be not-null, shouldn't it set the default value for those to string.Empty? If the current behavior is intended, can I configure it somehow to use string.Empty instead of null?

The issue I'm having is when writing these objects to database using CsvHelper. Since some properties are set to Null when the database doesn't allow nulls, it causes an error. Letting them default to string.Empty instead seems it would fix that.

Example table

CREATE TABLE `aaaPerson` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `FirstName` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
    `MiddleName` VARCHAR(50) NULL DEFAULT NULL COLLATE 'latin1_swedish_ci',
    `LastName` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
    `Nickname` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'latin1_swedish_ci',
    PRIMARY KEY (`id`) USING BTREE
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;

EfPowerTools creates this Class:

[Table("aaaPerson")]
public partial class aaaPerson
{
    [Key]
    [Column(TypeName = "int(11)")]
    public int id { get; set; }

    [StringLength(50)]
    public string FirstName { get; set; } = null!;

    [StringLength(50)]
    public string? MiddleName { get; set; }

    [StringLength(50)]
    public string LastName { get; set; } = null!;
}

Provide technical details

ErikEJ commented 1 month ago

Sounds like an issue with your input data / csvhelper that is entirely unrelated to EF Core...

Germs2004 commented 1 month ago

It's ok to close this issue if you think it's best. I just thought it was weird that a Non-Null property was being set to Null. I've fixed my issue another way, but sincerely, thank you for the great tool.

ErikEJ commented 1 month ago

I will go ahead a close, since you found a workaround.

If you like my free tools, I would be very grateful for a rating or review on Visual Studio Marketplace or even a one-time or monthly sponsorship