Adding CreatedDate to an entity using Entity Framework 5 Code First

后端 未结 7 500
心在旅途
心在旅途 2020-12-13 02:24

I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UT

相关标签:
7条回答
  • 2020-12-13 02:58

    Code First doesn't currently provide a mechanism for providing column default values.

    You will need to manually modify or create base class to automatic update CreatedDate

    public abstract class MyBaseClass
    {
        public MyBaseClass()
        {
            CreatedDate = DateTime.Now;
        }
        public Datetime CreatedDate { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-13 03:02

    Similar to Stephans's Answer but with Reflection and also ignores all user (external) updates Created/Updated times. Show Gist

      public override int SaveChanges()
            {
                foreach (var entry in ChangeTracker.Entries().Where(x => x.Entity.GetType().GetProperty("CreatedTime") != null))
                {
                    if (entry.State == EntityState.Added)
                    {
                        entry.Property("CreatedTime").CurrentValue = DateTime.Now;
                    }
                    else if (entry.State == EntityState.Modified)
                    {
                        // Ignore the CreatedTime updates on Modified entities. 
                        entry.Property("CreatedTime").IsModified = false;
                    }
    
                    // Always set UpdatedTime. Assuming all entities having CreatedTime property
                    // Also have UpdatedTime
                    // entry.Property("UpdatedTime").CurrentValue = DateTime.Now;
                    // I moved this part to another foreach loop
                }
    
                foreach (var entry in ChangeTracker.Entries().Where(
                    e => 
                        e.Entity.GetType().GetProperty("UpdatedTime") != null && 
                        e.State == EntityState.Modified || 
                        e.State == EntityState.Added))
                {
                    entry.Property("UpdatedTime").CurrentValue = DateTime.Now;
                }
    
                return base.SaveChanges();
            }
    
    0 讨论(0)
  • 2020-12-13 03:07

    Ok so the primary issue here was that CreatedDate was being Updated every time I called SaveChanges and since I wasn't passing CreatedDate to my views it was being updated to NULL or MinDate by Entity Framework.

    The solution was simple, knowing that I only need to set the CreatedDate when EntityState.Added, I just set my entity.CreatedDate.IsModified = false before doing any work in my SaveChanges override, that way I ignored changes from Updates and if it was an Add the CreatedDate would be set a few lines later.

    0 讨论(0)
  • 2020-12-13 03:10

    Override the SaveChanges-Method in your context:

    public override int SaveChanges()
    {
      DateTime saveTime = DateTime.UtcNow;
      foreach (var entry in this.ChangeTracker.Entries().Where(e => e.State == (EntityState) System.Data.EntityState.Added))
       {
         if (entry.Property("CreatedDate").CurrentValue == null)
           entry.Property("CreatedDate").CurrentValue = saveTime;
        }
        return base.SaveChanges();
    
    }
    

    Updated because of comments: only freshly added Entities will have their Date set.

    0 讨论(0)
  • 2020-12-13 03:13

    For EF Core you can find the MS recommended solution here: Default Values.

    Use Fluent API in your DBContext:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property(b => b.Created)
            .HasDefaultValueSql("getdate()");
    }
    
    0 讨论(0)
  • 2020-12-13 03:14
    Accounts account;
    account.Acct_JoinDate = DateTime.Now.ToUniversalTime();
    data.Accounts.Add(account);
    data.SaveChanges();
    

    Why not give the timestamp upon model creation? Similar to these accounts here.

    0 讨论(0)
提交回复
热议问题