如何自动填充CreatedDate和ModifiedDate? [英] How to automatically populate CreatedDate and ModifiedDate?

查看:147
本文介绍了如何自动填充CreatedDate和ModifiedDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习ASP.NET Core MVC,而我的模型是

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

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


        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }
    }
}

Passage表用于保存我写的段落.

The Passage table is used to save passages I wrote.

  • Create视图只有一个字段Contents可以输入段落.服务器必须自动将CreatedDateModifiedDate设置为相等(使用UTC格式).

  • Create view just has one field Contents to input a passage. CreatedDate and ModifiedDate must be automatically set equal by the server (using UTC format).

Edit视图只有一个字段Contents用于编辑段落. ModifiedDate必须由服务器自动设置.

Edit view just has one field Contents to edit a passage. ModifiedDate must be automatically set by the server.

我必须为CreatedDateModifiedDate属性附加哪些属性,以使服务器根据上述情况自动填充这些属性?

What attributes I have to attach to the CreatedDate and ModifiedDate properties to make them automatically populated by the server based on the above scenario?

推荐答案

我必须将哪些属性附加到CreatedDate和ModifiedDate属性,以使服务器根据上述情况自动填充它们?

What attributes I have to attach to the CreatedDate and ModifiedDate properties to make them automatically populated by the server based on the above scenario?

解决方案1)

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


        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

       public Passage()
       {          
         this.CreatedDate  = DateTime.UtcNow;
         this.ModifiedDate = DateTime.UtcNow;
       }
    }
}

并且通过编辑,您必须自行更改/更新它!

and by edit you have to change/update it by your self!

解决方案2)

自定义属性:

[SqlDefaultValue(DefaultValue = "getutcdate()")]
public DateTime CreatedDate { get; set; }

实体框架6代码优先默认值

解决方案3)

借助计算机:

[Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedUtc { get; set; 


  "dbo.Products",
            c => new
                {
                    ProductId = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    CreatedUtc = c.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"),
                })
            .PrimaryKey(t => t.ProductId);

解决方案4) 您也可以通过手动修改查询来使用命令拦截器执行此操作.

Solution 4) You can also do this with command interceptor by modifying manually the query.

解决方案5) 使用存储库模式来管理数据创建并通过CreateNew进行设置 这是我最喜欢的解决方案!

Solution 5) Use Repository pattern to manage the data creation and set it by CreateNew This is my favour Solution!

https://msdn.microsoft.com/en-us/library/ff649690.aspx

解决方案6) 只需进行设置或进入用户界面或您的VM.

Solution 6) just set it or get in in the UI or in your VM.

Entity Framework Core 1.0 中简单:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Passage>()
        .Property(b => b.CreatedDate )
        .HasDefaultValueSql("getdate()");
}

这篇关于如何自动填充CreatedDate和ModifiedDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆