Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Problem with generate DateTime with Entity Framework

$
0
0

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.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>