Search code examples
databasemodelentity-framework-coreef-code-firstasp.net-web-api2

Cannot insert the value NULL into column 'created_date', table 'WebApp.dbo.Inventories';


I created a model BaseModel to inherit these 2 properties date_created and date_modified to all models in my database to keep a record when a record is created and when a record is modified.

I added the ? operator for these properties to accept null values ​​as shown below:

Base model:

namespace API.Models
{
    public class BaseModel
    {
        public BaseModel()
        {
            created_date = DateTime.Now;
        }

        public DateTime? created_date { get; set; }
        public DateTime? modified_date { get; set; }
    }
}

Inventories table:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;

namespace API.Models
{
    public class Inventory : BaseModel
    {
        [Key]
        public int inventory_id { get; set; }
        [Required(ErrorMessage = "El campo {0} es obligatorio"), StringLength(10, ErrorMessage = "{0} la longitud debe estar entre {2} y {1}.", MinimumLength = 5)]
        public string name { get; set; }
        public string? location { get; set; }
        public bool? status { get; set; }

        public List<Asset> assets { get; set; }
    }
}

Asset table:

using System.ComponentModel.DataAnnotations;

namespace API.Models
{
    public class Asset : BaseModel
    {
        [Key]
        public int asset_id { get; set; }
        public int? code { get; set; }
        [Required(ErrorMessage = "El campo {0} es obligatorio"), StringLength(10, ErrorMessage = "{0} la longitud debe estar entre {2} y {1}.", MinimumLength = 5)]
        public string? name { get; set; }
        public int iventory_id { get; set; }
        public Inventory inventory { get; set; }
    }
}

Then I add the migrations

dotnet ef migrations add v15

and apply the migrations

dotnet ef database update

I am getting the following error:

Cannot insert the value NULL into column 'created_date', table 'WebApp.dbo.Iventories'; column does not allow nulls. UPDATE fails. The statement has been terminated

I don't understand why I am getting this error.

When I add ? operator should the database not accept null values​​?


Solution

  • Please use these

    public class BaseModel
    {
        public BaseModel()
        {
            created_date = DateTime.Now;
        }
    
        [DataType(DataType.DateTime)]
        public DateTime? created_date { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime? modified_date { get; set; }
    
    }
    

    or

    public class BaseModel
    {
        public BaseModel()
        {
            created_date = DateTime.Now;
        }
    
        public Nullable<DateTime> created_date { get; set; }
        public Nullable<DateTime> modified_date { get; set; }
    
    }
    

    I uplaod sample code in git, just change ConnectionString