How to automatically populate CreatedDate and ModifiedDate?

后端 未结 6 1992
失恋的感觉
失恋的感觉 2021-01-31 17:07

I am learning ASP.NET Core MVC and my model is

namespace Joukyuu.Models
{
    public class Passage
    {
        public int PassageId { get; set; }
        publi         


        
6条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 17:46

    Its too easy.Just you should do 2 step:

    1.create model with these fields on top of createDate field:

     [Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    

    2.After create migration and in its file befor update database edit column of this part and add defaultValueSql: "getdate()" like this:

     id = table.Column(nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CreatedDate = table.Column(defaultValueSql: "getdate()",nullable: false)
       
    

提交回复
热议问题