8

I am working on a project and I would like to set some properties to optional in code first approach. In my class file I have added [Required] to properties I would set to not null in SQL and properties that do not have [Required] should set to allow null in SQL, but for some properties that contains ID in it, it sets to not null in SQL. Below is my sample class that contains properties that should be used when generating tables in the database. I would like to set EnvironmentLastDeployedId property to optional when it creates new database in MSSQL.

public class Version
{
    public int Id { get; set; }

    [Required]
    public string PackageName { get; set; }
    public string VersionName { get; set; }
    public string OriginalPackageName { get; set; }

    [Required]
    public string CommitID { get; set; }
    public string CommitMessage { get; set; }
    public int ApplicationId { get; set; }
    public Application Application { get; set; }
    public string CreatedBy { get; set; }
    public int EnvironmentLastDeployedId { get; set; }
    public Environment EnvironmentLastDeployed { get; set; }
    public int StatusId { get; set; }
    public Status Status { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }
}

In my Context class file, I tried to use Fluent API, but I only get IsRequired method, but not IsOptional or something that allows me to set table column to allow null in database.

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Version>()
            .Property(p => p.EnvironmentLastDeployedId).IsRequired();
    }
Ray
  • 1,095
  • 3
  • 18
  • 43

2 Answers2

14

If you want to allow null values for int you just need to use int?:

public int? ApplicationId { get; set; }

This will map to a nullable column in SQL, so it won't throw any exception when trying to store a record with a null value for that column.

joaoruimartins
  • 537
  • 4
  • 14
  • Thanks for your response. I am already using `public int EnvironmentLastDeployedId { get; set; }` in my class file. But for some reason it EF7 it created not null column in SQL server. – Ray Feb 05 '16 at 16:38
  • Ohh, so you mean to add question mark and it will set it to nullable? `public int? EnvironmentLastDeployedId { get; set; }` – Ray Feb 05 '16 at 16:40
  • Yeah, that's what I said. Not int, but int? which is a nullable type. – joaoruimartins Feb 05 '16 at 16:40
  • 4
    For `string` types you are not allowed to write `public string? myString { get; set; }` Because string types are already `nullable`. Just add `[Required]` before them if you want they don't be optional. – Masoud Keshavarz Jan 29 '18 at 06:35
  • 2
    @masoud-keshavarz This doc speaks of "string?" - If nullable reference types are enabled, properties will be configured based on the C# nullability of their .NET type: string? will be configured as optional, whereas string will be configured as required. https://learn.microsoft.com/en-us/ef/core/modeling/required-optional?tabs=without-nrt%2Cdata-annotations – George Birbilis Dec 17 '19 at 11:09
  • ...however other MS doc says differently: By convention, a property whose CLR type can contain null will be configured as optional (string, int?, byte[], etc.). Properties whose CLR type cannot contain null will be configured as required (int, decimal, bool, etc.).Note A property whose CLR type cannot contain null cannot be configured as optional. The property will always be considered required by Entity Framework. https://ef.readthedocs.io/en/staging/modeling/required-optional.html#conventions – George Birbilis Dec 17 '19 at 11:13
  • ...maybe related to #nullable enable of C#8 - https://stackoverflow.com/a/57756254/903783 – George Birbilis Dec 17 '19 at 11:15
  • 1
    @masoud-keshavarz sorry for the confusion, indeed that article says "C# 8 introduced a new feature called nullable reference types, which allows reference types to be annotated, indicating whether it is valid for them to contain null or not. This feature is disabled by default, and if enabled, it modifies EF Core's behavior in the following way:", so one has to enable that behavior – George Birbilis Dec 17 '19 at 11:21
-1

.IsRequired() has an override constructor. For instance to make a column nullable you set the .IsRequired(false);

cgcarter1
  • 327
  • 1
  • 8