Nullable property to entity field, Entity Framework through Code First

后端 未结 4 1629
一整个雨季
一整个雨季 2020-12-05 03:32

Using the data annotation Required like so:

[Required]
public int somefield {get; set;}

Will set somefield to Not

4条回答
  •  再見小時候
    2020-12-05 04:11

    Just omit the [Required] attribute from the string somefield property. This will make it create a NULLable column in the db.

    To make int types allow NULLs in the database, they must be declared as nullable ints in the model:

    // an int can never be null, so it will be created as NOT NULL in db
    public int someintfield { get; set; }
    
    // to have a nullable int, you need to declare it as an int?
    // or as a System.Nullable
    public int? somenullableintfield { get; set; }
    public System.Nullable someothernullableintfield { get; set; }
    

提交回复
热议问题