locked
[NotNull] Entity Attribute not working RRS feed

  • Question

  • Hi everyone,

    I have two differents behavior when I use Entity attribute on my Code First project.

    Here is the part of the code :

    [Required]
    [Column("Email")]
    [EmailAddress]
    [Display(Name = "Email")]
    public string ProxyAddresses { get; set; }
    
    [Column("BirthDate")]
    [Display(Name = "Birthdate")]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }
    
    [Column("AccountExpires", TypeName = "DateTime2")]
    [Display(Name = "Expiration Date")]
    [AllowNull]
    [MaybeNull]
    public DateTime AccountExpires { get; set; }

    The ProxyAddress field is correctly set to allow Null (or not if I add [Required] attribute). But for the others, it does not work. Even if I add [AllowNull] and [MaybeNull], the SSMS shows me that the fields are not allowed to be null.

    Can you help me to find the reason of this behavior ?

    Regards,

    Arnaud H.


    The key of learning is practice.

    • Moved by CoolDadTx Wednesday, May 6, 2020 2:03 PM EF related
    Wednesday, May 6, 2020 8:31 AM

Answers

  • Hello,

    Since these attributes are new to EF Core 3 there is a chance there is an issue so you should ask over on GitHub.

    https://github.com/dotnet/efcore/issues 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by arnaud.helin Thursday, May 7, 2020 4:09 AM
    Wednesday, May 6, 2020 9:54 AM
  • Hi,

    For those who are interested, I posted the issue and it was a normal behavior, here is what the github said :

    "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."



    The key of learning is practice.

    • Marked as answer by arnaud.helin Tuesday, August 11, 2020 11:46 AM
    Tuesday, August 11, 2020 11:46 AM

All replies

  • Hello,

    Since these attributes are new to EF Core 3 there is a chance there is an issue so you should ask over on GitHub.

    https://github.com/dotnet/efcore/issues 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by arnaud.helin Thursday, May 7, 2020 4:09 AM
    Wednesday, May 6, 2020 9:54 AM
  • Hi Karen,

    Thank you for your answer, i will post the issue immediately.

    Regards


    The key of learning is practice.

    Wednesday, May 6, 2020 4:51 PM
  • Hi,

    For those who are interested, I posted the issue and it was a normal behavior, here is what the github said :

    "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."



    The key of learning is practice.

    • Marked as answer by arnaud.helin Tuesday, August 11, 2020 11:46 AM
    Tuesday, August 11, 2020 11:46 AM