I am trying to model a chat box, therefore I want the time a Message created is generated by the database. This is myMessage class:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace DatabaseGenerated.Models { public class Message { public int ID { get; set; } [Required] public string Content { get; set; } [Required] [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime CreatedTime { get; set; } } }
After I use Visual Studio to scaffolding the controller and update the database, I tried toCreate a Message. However, I received an error:
SqlException: Cannot insert the value NULL into column 'CreatedTime', table 'DatabaseGeneratedContext-GUID.dbo.Message'; column does not allow nulls. INSERT fails.
I guess the problem is the database cannot generated the current time, so it insertNULL value.
Please suggest me a solution for this. Thank you.