<bdo id='bhnch'></bdo><ul id='bhnch'></ul>
  • <small id='bhnch'></small><noframes id='bhnch'>

      1. <tfoot id='bhnch'></tfoot>
        <i id='bhnch'><tr id='bhnch'><dt id='bhnch'><q id='bhnch'><span id='bhnch'><b id='bhnch'><form id='bhnch'><ins id='bhnch'></ins><ul id='bhnch'></ul><sub id='bhnch'></sub></form><legend id='bhnch'></legend><bdo id='bhnch'><pre id='bhnch'><center id='bhnch'></center></pre></bdo></b><th id='bhnch'></th></span></q></dt></tr></i><div id='bhnch'><tfoot id='bhnch'></tfoot><dl id='bhnch'><fieldset id='bhnch'></fieldset></dl></div>
        <legend id='bhnch'><style id='bhnch'><dir id='bhnch'><q id='bhnch'></q></dir></style></legend>
      2. 如何自动填充 CreatedDate 和 ModifiedDate?

        How to automatically populate CreatedDate and ModifiedDate?(如何自动填充 CreatedDate 和 ModifiedDate?)
          <tbody id='axD2S'></tbody>

        • <i id='axD2S'><tr id='axD2S'><dt id='axD2S'><q id='axD2S'><span id='axD2S'><b id='axD2S'><form id='axD2S'><ins id='axD2S'></ins><ul id='axD2S'></ul><sub id='axD2S'></sub></form><legend id='axD2S'></legend><bdo id='axD2S'><pre id='axD2S'><center id='axD2S'></center></pre></bdo></b><th id='axD2S'></th></span></q></dt></tr></i><div id='axD2S'><tfoot id='axD2S'></tfoot><dl id='axD2S'><fieldset id='axD2S'></fieldset></dl></div>
              <bdo id='axD2S'></bdo><ul id='axD2S'></ul>

              <small id='axD2S'></small><noframes id='axD2S'>

            • <tfoot id='axD2S'></tfoot>

                <legend id='axD2S'><style id='axD2S'><dir id='axD2S'><q id='axD2S'></q></dir></style></legend>
                  本文介绍了如何自动填充 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);
                  

                  https://andy.mehalick.com/2014/02/06/ef6-adding-a-created-datetime-column-automatically-with-code-first-migrations/

                  解决方案 4)您也可以通过手动修改查询来使用命令拦截器来做到这一点.

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

                  解决方案 5)使用 Repository 模式管理数据创建并由 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)只需在 UI 或 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                  <small id='0199C'></small><noframes id='0199C'>

                      <legend id='0199C'><style id='0199C'><dir id='0199C'><q id='0199C'></q></dir></style></legend>

                        <tfoot id='0199C'></tfoot>
                          <i id='0199C'><tr id='0199C'><dt id='0199C'><q id='0199C'><span id='0199C'><b id='0199C'><form id='0199C'><ins id='0199C'></ins><ul id='0199C'></ul><sub id='0199C'></sub></form><legend id='0199C'></legend><bdo id='0199C'><pre id='0199C'><center id='0199C'></center></pre></bdo></b><th id='0199C'></th></span></q></dt></tr></i><div id='0199C'><tfoot id='0199C'></tfoot><dl id='0199C'><fieldset id='0199C'></fieldset></dl></div>
                            <bdo id='0199C'></bdo><ul id='0199C'></ul>
                              <tbody id='0199C'></tbody>