question

BenjaminKempe-3537 avatar image
0 Votes"
BenjaminKempe-3537 asked karenpayneoregon answered

EF Core Alter Column size automatically

Is there any way, to resize the column to the needed length in runtime automatically?
for example: the column is a nvarchar(50), but the value to insert has a length of 80. Is it now possible to alter the column to nvarchar(80) by EF Core automatically?
Without EF Core we catch the Fieldlength-Exception and alter the columnsize to the needed length.

dotnet-entity-framework-core
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BenjaminKempe-3537, Welcome to Microsoft Q&A, I am sorry that Currently EF core doesn't support altering column size automatically. I recommend that you design the table at the beginning of making an app or use the AlterColumn method to update the column manually if you need to change the column size after designing the table.


0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Set the column length to say 255 than use FluentValidation RuleFor to set the length at runtime in a class which inherits AbstractValidator. This of course means you validate before SaveChangess.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.