C
C#2y ago
MileHDev

❔ After I make a string nullable (by adding the ?) and add the migration then update my database...

It still shows as not nullable, why?
21 Replies
MileHDev
MileHDevOP2y ago
Using EntityFramework Still shows like this in the migration:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Title",
table: "Todos",
type: "nvarchar(50)",
maxLength: 50,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");

migrationBuilder.AlterColumn<string>(
name: "Description",
table: "Todos",
type: "nvarchar(100)",
maxLength: 100,
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldNullable: true);
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Title",
table: "Todos",
type: "nvarchar(50)",
maxLength: 50,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(max)");

migrationBuilder.AlterColumn<string>(
name: "Description",
table: "Todos",
type: "nvarchar(100)",
maxLength: 100,
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldNullable: true);
}
public string? Title { get; set; }
public string? Title { get; set; }
Angius
Angius2y ago
You don't have any config that'd contradict that nullability?
MileHDev
MileHDevOP2y ago
Hmm. I don't think so. Where would I look?
Aaron
Aaron2y ago
the nullable part in AlterColumn is changed, no? it went from nullable: false to nullable: true note that string? isn't actually a unique generic type
MileHDev
MileHDevOP2y ago
theres two fiels here, title is still nullable false
Aaron
Aaron2y ago
oh, yeah, now I see I thought those were the same for some reason, must just be tired
MileHDev
MileHDevOP2y ago
no worries I have a required attribute on top of it/ Does that change anything? I thought it dind't from research I did
[Required]
[MaxLength(50)]
public string? Title { get; set; }
[Required]
[MaxLength(50)]
public string? Title { get; set; }
I thought that the required attribute only affected the validation of the model
Henkypenky
Henkypenky2y ago
hover over required, what's the class
MileHDev
MileHDevOP2y ago
MileHDev
MileHDevOP2y ago
Yeah, it's the required attribute.
Henkypenky
Henkypenky2y ago
did you enable NRT after the fact? was it disabled before?
MileHDev
MileHDevOP2y ago
NRT?
Henkypenky
Henkypenky2y ago
nullable reference types or is this new
MileHDev
MileHDevOP2y ago
this? <Nullable>enable</Nullable> It's been there from the start I'm goign to remove the attribute and see what happens
Henkypenky
Henkypenky2y ago
it should work with both, besides you need required right for validations
MileHDev
MileHDevOP2y ago
Yup. It's nullable now. Really should have tried that before. I removed the attribute. So it's not working as it should you think?
Henkypenky
Henkypenky2y ago
it seems as they are incompatible so you can do this
Henkypenky
Henkypenky2y ago
Stack Overflow
Nullable property to entity field, Entity Framework through Code First
Using the data annotation Required like so: [Required] public int somefield {get; set;} Will set somefield to Not Null in database, How can I set somefield to allow NULLs?, I tried setting it thr...
Henkypenky
Henkypenky2y ago
to be required, and nullable in the database but it makes little sense, if you logic prevents it from being null, why would it be nullable see the discrepancy ? maybe that's the logic behind it
MileHDev
MileHDevOP2y ago
Yes. I was following some notes from a tuto I saw a while back. Logically, I thought about that but... I don't know enough to contradict it. But yeah, I think this is fine. I don't need it to be nullable. thank you everybody
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server