Home > Enterprise >  How to send a null value to a not null column in ASP.NET with LINQ
How to send a null value to a not null column in ASP.NET with LINQ

Time:09-17

my problem is this:

  • I have a table in SQL Server that have a NOT NULL Column that uses a SEQUENCE as default (NEXT VALUE FOR SeqDefa), that column is not the PK from the table.

  • I building a web form in ASP.NET that does a CRUD in that table, when i want to send a null value in that column to use the default in that table i get an error. that's because LINQ says that you can't send a null value to a not null column

how can i fix it?

can i get the sequence´s next value from LINQ in my form? there's another option than changing table column from not null to allowing nulls?

CodePudding user response:

If you are using Entity Framework, then you might be looking for the [DatabaseGenerated] attribute or .ValueGeneratedOnAdd() in the fluent API.

Microsoft Documentation

CodePudding user response:

If You are using EF core, You could also look at Value converters.
Write a simple one that will set chosen default value when given value is null, or some calculated value if You prefer.
Thanks to this You there will be no more need to have some special behaviour in your linq code - just normally use null when needed and value converter will take care of the rest.

  • Related