Closed arnaudhelin closed 2 years ago
@arnaudhelin EF doesn't currently read the AllowNull
or MaybeNull
attributes. EF interprets Required
to mean make the column non-nullable, the same as if you specify Required
in the model building API.
@arnaudhelin you seem to be applying [AllowNull]
and [MaybeNull]
to DateTime, which is a value type - these attributes are meant for use only for reference types (they were introduced in C# 8 for the new nullable reference types feature). Just as in C# the compiler doesn't make your property nullable because of these attributes, EF Core doesn't respect them either.
For value types like DateTime, you can make the property nullable in C# by specifying DateTime?
.
Thank you both. Effectively I tried with the ? and it works.
Regards
Description of the issue
The decorating attributes in the model do not apply correctly. The [AllowNull] do not change the property correctly. The field in the schema database still stay as NotNull. By the way, the same odd behavior appears in other field on which the [Required] attribute do not apply.
Steps to reproduce
The ProxyAddress property is correctly set to not allow Null (or allow null if I remove [Required] attribute). But for the others, it does not work at all, seems like it is completely skipped. Even if I add [AllowNull] and [MaybeNull], the SSMS shows me that properties are not allowed to be null.
Exceptions
During the build, I have some exceptions, but I can't determine why I get these now.
I wonder if I had to install the Cryptography dll missing by myself (nuget directly) but it is written below the package, that it is recommended to not add the reference directly.
T-SQL create by EF (Use dbforge by Devart)
As you can see there is a NOT NULL value which should not be there.
Further technical details
EF Core version: 3.1.3 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: netcoreapp3.1 Operating system: Windows 10 Pro Insider Preview (build 19592) IDE: Visual Studio Community 2019 16.5.0